-2

I'm trying to figure out how I can check if a date is 24 hours or less from Now.

For example I have a date 11/2/2022 and several days has passed now. But I want to do a check for instance if my date is 11/11/2022 and if I check now since it's less than 24 hours will return true.

var mylast = 1667446218000;
var now = Date.now();

if(new Date(mylast) > Date.now() - 24 * 60 * 60 * 1000) {
    console.log('true');
}
Eric Evans
  • 640
  • 2
  • 13
  • 31

1 Answers1

-1

const MS_IN_DAY = 86_400_000

// NOTE: month is zero-based
const upto24HoursAwayLocal = (year, month, day) => {
  const now = +(new Date) // now as an instant (internally UTC)
  const then = +(new Date(year, month, day)) // local date, converted to UTC instant internally
  const difference = then - now
  return difference > 0 && difference <= MS_IN_DAY
}

console.log(upto24HoursAwayLocal(2022, 10, 13))
Ben Aston
  • 53,718
  • 65
  • 205
  • 331