Check if there are 24hrs between two dates in Javscript (js)

PHOTO EMBED

Mon Apr 25 2022 17:14:23 GMT+0000 (UTC)

Saved by @lancerunsite #javascript #dates #24h #js

const then = new Date('2022-01-24T09:30:20');
const now = new Date();

const msBetweenDates = Math.abs(then.getTime() - now.getTime());

// 👇️ convert ms to hours                  min  sec   ms
const hoursBetweenDates = msBetweenDates / (60 * 60 * 1000);

console.log(hoursBetweenDates);

if (hoursBetweenDates < 24) {
  console.log('date is within 24 hours');
} else {
  console.log('date is NOT within 24 hours');
}
content_copyCOPY