Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

javascript - Check if a date is 24 hours old

I have a created date. I want to run an if/switch statement to know whether the date and time right now is more than 24 hours from the created date.

If the created date is more than 24 hours ago, do something, if it's less than 24 hours ago do something else.

let now = +new Date();

// This is returned as: July 18, 2018 at 3:48:00 AM UTC+1
let createdAt = +doc.data().created_at;

const oneDay = 60 * 60 * 24;
var compareDatesBoolean = (now - createdAt) > oneDay;

Any guidance here would be great thank you! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Created Example For One hour Refer This and Then Change it to 24 Hours

const OneHourAgo= (date) => {
    const hour= 1000 * 60 * 60;
    const hourago= Date.now() - hour;

    return date > hourago;
}

Simple One:-

var OneDay = new Date().getTime() + (1 * 24 * 60 * 60 * 1000)
                                     day hour  min  sec  msec
if (OneDay > yourDate) {
    // The yourDate time is less than 1 days from now
}
else if (OneDay < yourDate) {
    // The yourDate time is more than 1 days from now
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...