Check if a Date is less than 24 Hours ago using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 26, 2022

banner

Photo from Unsplash

Check if a Date is less than 24 Hours ago using JavaScript #

To check if a date is less than 24 hours ago:

  1. Convert 24 hours to milliseconds.
  2. Get a timestamp that represents the time 24 hours ago.
  3. Check if the date's timestamp is greater than the timestamp 24 hours ago.
index.js
function isLessThan24HourAgo(date) { // 👇️ hour min sec milliseconds const twentyFourHrInMs = 24 * 60 * 60 * 1000; const twentyFourHoursAgo = Date.now() - twentyFourHrInMs; return date > twentyFourHoursAgo; } console.log(isLessThan24HourAgo(new Date())); // 👉️ true console.log(isLessThan24HourAgo(new Date('2022-01-25'))); // 👉️ false

We created a reusable function that takes a Date object as a parameter and checks if the date is less than 24 hours ago.

The function in the example would also return true for dates in the future. If you only want to check if the Date is less than 24 hours ago and is NOT in the future, scroll down to the next code snippet.

The first step is to calculate how many milliseconds there are in 24 hours.

The Date.now() method returns the number of milliseconds elapsed since midnight of the 1st of January 1970.

By subtracting 24 hours in milliseconds from the timestamp, we get the moment in time 24 hours ago.

If the passed in date is greater than the timestamp of 24 hours ago, than the date is less than 24 hours old or is in the future.

We are able to compare the date to a timestamp because under the hood each date stores a timestamp - the number of milliseconds elapsed between the 1st of January 1970 and the given date.

index.js
const date = new Date('2022-07-24'); // 👇️ 1658620800000 console.log(date.getTime());
Each date stores a timestamp under the hood, so the default behavior is to compare the timestamps of the dates, even if you don't explicitly call the getTime() method on each date.

If you need to check if a date is less than 24 hours ago and is not in the future, add a condition that checks that the date is less than or equal to the current timestamp.

index.js
function isLessThan24HourAgo(date) { // 👇️ hour min sec milliseconds const twentyFourHrInMs = 24 * 60 * 60 * 1000; const twentyFourHoursAgo = Date.now() - twentyFourHrInMs; return date > twentyFourHoursAgo && date <= Date.now(); } console.log(isLessThan24HourAgo(new Date())); // 👉️ true console.log(isLessThan24HourAgo(new Date('2045-09-24'))); // 👉️ false
Our first condition checks that the date's timestamp is greater than the timestamp 24 hours ago and the second condition checks that the date's timestamp is less than or equal to the current timestamp.

The second condition would not be met for any dates in the future.

The logical AND (&&) operator will only return true if both conditions are met, otherwise false is returned.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.