简体   繁体   中英

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! :)

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
}

Add this const oneday = 60 * 60 * 24 * 1000 (milliseconds) . Since, JS converts difference between 2 Datetime objects into milliseconds. I think that should work

Using moment will be much easier in this case, You could try this:

    let hours = moment().diff(moment(yourDateString), 'hours');
    let days = moment().diff(moment(yourDateString), 'days');

It will give you integer value like 1,2,5,0etc so you can easily use condition check like:

if(hours >= 24) { your code }

or

if(days >= 1) { your code }

Also, one more thing is you can get more accurate result of the time difference (in decimals like 1.2,1.5,0.7etc) to get this kind of result we need to pass 3rd argument which ie true for precise result use this syntax:

let hours = moment().diff(moment(yourDateString), 'hours', true);

Let me know if you have any further query

Maybe something like that?

const now = Date.now();

// if it's a firebase timestamp
const createdAt = doc.data().created_at.toMillis(); 

const oneDay = 24 * 60 * 60 * 1000;

const isMoreThanADay = (now - createdAt) > oneDay;

I don't know firebase timestamps I relied on the documentation for the toMillis method.

If you want to check any date from DB (in mysql format or any other) is not older then last 24 hours (in case of expire reset password link) you can use the following logic where in my case user.passwordTokenTimestamp is value save in DB

let now = +new Date();
let createdAt = +new Date(Date.parse(user.passwordTokenTimestamp.toString()));
var oneDay = 24 * 60 * 60 * 1000;

if ((now - createdAt) > oneDay) {
  const errors = {
    User: 'Password reset token has expired, please resend forgot password email !',
  };
  throw new HttpException({ errors }, HttpStatus.UNPROCESSABLE_ENTITY);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM