Check if a Date is less than 1 Hour ago using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 26, 2022

banner

Photo from Unsplash

Check if a Date is less than 1 Hour ago using JavaScript #

To check if a date is less than 1 hour ago:

  1. Convert 1 hour to milliseconds.
  2. Get a timestamp that represents the time 1 hour ago.
  3. Check if the date's timestamp is greater than the timestamp 1 hour ago.
index.js
function isLessThan1HourAgo(date) { // 👇️ min sec milliseconds const hourInMilliseconds = 60 * 60 * 1000; const oneHourAgo = Date.now() - hourInMilliseconds; return date > oneHourAgo; } console.log(isLessThan1HourAgo(new Date())); // 👉️ true console.log(isLessThan1HourAgo(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 1 hour 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 1 hour 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 an hour.

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

By subtracting 1 hour in milliseconds from the timestamp, we get the moment in time 1 hour ago.

If the passed in date is greater than the timestamp of 1 hour ago, than the date is less than 1 hour 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-09-24'); // 👇️ 1663977600000 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 1 hour 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 isLessThan1HourAgo(date) { // 👇️ min sec milliseconds const hourInMilliseconds = 60 * 60 * 1000; const oneHourAgo = Date.now() - hourInMilliseconds; return date > oneHourAgo && date <= Date.now(); } console.log(isLessThan1HourAgo(new Date())); // 👉️ true console.log(isLessThan1HourAgo(new Date('2035-01-29'))); // 👉️ false
Our first condition checks that the date's timestamp is greater than the timestamp 1 hour 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.