Home > Web Front-end > JS Tutorial > body text

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

王林
Release: 2023-08-25 09:05:02
forward
1555 people have browsed it

如何使用 JavaScript 检查日期是否小于 1 小时前?

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 have to work with Date objects and perform some operations every hour. So this could be a way to check if a specific action was performed an hour ago and then do it again; otherwise, wait for that hour to complete.

Here we will learn different ways to check if a date is less than 1 hour ago using JavaScript.

Use getTime() method

The getTime() method returns the total number of milliseconds in the date since January 1, 1970. Therefore, we can use the getTime() method to find the total number of milliseconds since January 1, 1970 for the current and previous date. After that, we can find the difference in the total milliseconds of the two dates and compare it with the total milliseconds of the hour, which is 1000*60*60.

grammar

Users can use JavaScript to check whether the date is less than 1 hour ago using the following syntax.

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
}
Copy after login

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

step

Step 1 - Create two dates.

Step 2 - Get the total milliseconds of two dates using the getTime() method, find the difference between them and store its value in the difference variable.

Step 3 - Store the total number of milliseconds for 1 hour in the hoursMilli variable.

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

Example

In the example below, we create two different dates with different timestamps. We follow the steps above and compare the date and the total milliseconds of current_date with the total milliseconds of 1 hour to check if the date is less than an 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> 
Copy after login

Use the setMinutes() method of the date object

The

setMinutes() method in the Date object allows developers to set the minutes in the timestamp. The date requires three parameters: minutes, seconds, and milliseconds to set. Seconds and milliseconds are optional.

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

grammar

Users can use the setMinute() method according to the following syntax to check whether the difference between two dates is less than one 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.
} 
Copy after login

step

Step 1 - Use the setMinutes() method and pass 0, 0, 0 as arguments to set the minutes, seconds and milliseconds to zero > previous date.

Step 2 - Set the minutes of current_date to zero.

Step 3 - Calculate the difference between the two dates; if it is zero, the date was less than an hour ago.

Example

In this example, we use the setMinutes() method to set the date and the total number of minutes to zero. After that, we take the difference between the dates and return the difference in milliseconds between the dates.

If the difference is zero, the year, month, day, and hour of the two dates are the same. Therefore, we can say that this day is 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>
Copy after login

In this tutorial, we learned to find the difference between two dates and check if the date was less than 1 hour ago. Additionally, 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 now deprecated.

The above is the detailed content of How to check if a date is less than 1 hour ago using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template