How to check if the date is less than 1 hour ago using JavaScript?


In this tutorial, we will learn to find the difference between two dates and check if the difference is less than one hour. Sometimes, developers must play with the Date object and perform some operation every hour. So, this can be one approach that checks if a particular operation is performed before an hour, perform it again; otherwise, wait to complete the hour.

Here, we will learn different approaches to check if the date is less than 1 hour ago using JavaScript.

Use the getTime() method

The getTime() method returns the total milliseconds for the date from 1st January 1970. So, we can use the getTime() method to find the total milliseconds of the current and previous date. After that, we can find the difference in the total milliseconds of both dates and compare it with the total milliseconds of the hours, which is 1000*60*60.

Syntax

Users can follow the syntax below to check if the date is less than 1 hour ago using JavaScript.

let current_date = new Date();
let difference = current_date.getTime() - date.getTime();
let hoursMilli = 1000 * 60 * 60; // milliseconds * seconds * minutes
if (Math.abs(difference) < hoursMilli) {
   
   // less than 1 hour spent
} else {
   
   // more than 1 hour ago
}

In the above syntax, hoursMilli contains the total milliseconds of hours, and we used the Math.abs() method to get the absolute difference of milliseconds between two dates.

Steps

Step 1 − Create two dates.

Step 2 − Use the getTime() method to get the total milliseconds for both dates, find the difference between them, and store its value in the difference variable.

Step 3 − Store the total milliseconds of 1 hour in the hoursMilli variable.

Step 4 − If the absolute value of the difference variable is less than hourMilli, that means 1 hour is not spent yet.

Example

In the example below, we have created two different dates with different timestamps. We compare the difference of total milliseconds of date and current_date with the total milliseconds of 1 hour as stated in the above steps to check if the date was less than one hour ago.

<html>
<body>
   <h3>Using the <i> custom algorithm </i> to check if the date was less than 1 hour ago.</h2>
   <div id="output"> </div>
   <script>
      let output = document.getElementById("output");
      let date1 = new Date(2023, 02, 11);
      
      // Creating a date that is not less than 1 hour ago
      let date2 = new Date(new Date().getTime() - 20302);   
      function isHourSpent(date) {
         let current_date = new Date();
         let difference = current_date.getTime() - date.getTime();
         console.log(difference);
         let hoursMilli = 1000 * 60 * 60;
         
         // comparing the hour's total milliseconds and the difference between the two dates.
         if (Math.abs(difference) < hoursMilli) {
            output.innerHTML += "The " + date + " is less than an hour ago! <br/>";
         } else {
            output.innerHTML += "The " + date + " is not less than hour ago! <br/>";
         }
      }
      isHourSpent(date1);
      isHourSpent(date2);
   </script>
</body>
</html> 

Use the setMinutes() method of the Date Object

The setMinutes() method in the Date object allows developers to set the minutes in the timestamp. It takes three parameters representing the minutes, seconds, and milliseconds to set for the date, and the seconds and milliseconds are optional.

If we set minutes to zero and compare the two dates, we can know if a particular date is before an hour ago.

Syntax

Users can follow the syntax below to use the setMinute() method to check if the difference between two dates is less than an hour.

date.setMinutes(0, 0, 0);
current_date.setMinutes(0, 0, 0);
if (date - current_date == 0) {
   
   // difference between the two dates is not more than an hour
} else {
   
   // difference between two dates more than an hour.
} 

Steps

Step 1 − Use the setMinutes() method, and pass 0, 0, 0 as an argument to set minutes, seconds, and milliseconds zero for a previous_date.

Step 2 − Set minutes zero for current_date also.

Step 3 − Take the difference between both dates; if it is zero, the date is less than an hour ago.

Example

In this example, we have used the setMinutes() method to set the total minutes for the dates and zero. After that, we take the difference of dates, which returns the difference of milliseconds between the dates.

If the difference is zero, the year, month, date, and hour of both dates are the same. So, we can say that day was less than 1 hour ago.

<html>
<body>
   <h3>Using the <i> setMinutes() method </i> to check if the date was less than 1 hour ago.</h2>
   <div id="output"> </div>
   <script>
      let output = document.getElementById("output");
      let date1 = new Date(2022, 12, 01);
      let date2 = new Date(new Date().getTime() - 20302);
      
      function isHourSpent(date) {
         let current_date = new Date();
         date.setMinutes(0, 0, 0);
         current_date.setMinutes(0, 0, 0);
         if (date - current_date == 0) {
            output.innerHTML += "The " + date + " is less than hour ago! <br/>";
         } else {
            output.innerHTML += "The " + date + " is not less than an hour ago! <br/>";
         }
      }
      isHourSpent(date1);
      isHourSpent(date2);
   </script>
</body>
</html>

We learned to find the difference between two dates and check if the date was less than 1 hour ago in this tutorial. Furthermore, users can use the diff() and isAfter() methods of the Moment Js library to check for the same, but users should keep in mind that the Moment JS library is deprecated nowadays.

Updated on: 10-Mar-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements