Check If A Date Is Less Than 1 Hour Ago Using JavaScript

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

This article will show you how to check if a Date is less than 1 hour ago using JavaScript with the help of two different solutions: using new Date() with Date.now() and using moment() with subtract().

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

Using new Date() with Date.now()

You can find the syntax of the new Date() here. The Date.now() method returns the current timestamp.

Syntax:

Date.now()

Suppose we have a date which is 9 pm of 14/12/2022; you can create a new Date object and then compare it to the timestamp of 1 hour ago:

// Create a Date object of 9 pm 14th December 2022
let myDate = new Date("14 Dec 2022 21:00");
console.log(myDate);

// Get the timestamp of 1 hour ago in milliseconds
let oneHourAgo = Date.now() - 3600 * 1000;
console.log(oneHourAgo);

// Check if myDate is less than 1 hour ago
console.log(myDate < oneHourAgo);

Output:

Wed Dec 14 2022 21:00:00 GMT+0700
1671018826058
false

The logic behind this method is straightforward. JavaScript stores dates as long integers representing the milliseconds after 1st January, 1970. We can compare the timestamp of the Date object which you want to check with the timestamp of one hour ago (Date.now() minus 3600*1000 milliseconds). If it is larger than that value, a Date was less than 1 hour ago. Otherwise, if it is smaller than the timestamp of 1 hour ago, it was less than 1 hour ago.

Using moment() with subtract()

Instead of returning a Date object, the moment() constructor returns a moment object, which works in the moment library. To import this library, please add the following script tag in the HTML document:

<html>
    <body>
        <p>Result</p>
        <p>Result</p>
        <p>Result</p>
        <script src="https://momentjs.com/downloads/moment.js"></script>
        <script type="text/javascript">
            let results = document.getElementsByTagName("p");
            
            // Create a Date object of 9 pm 14th December 2022
            let myDate = moment("14 Dec 2022 21:00");
            results[0].innerHTML = myDate;

            // Get the timestamp of 1 hour ago in milisecond
            let oneHourAgo = moment() - 3600 * 1000;
            results[1].innerHTML = oneHourAgo;

            // Check if myDate is less than 1 hour ago
            results[2].innerHTML = myDate < oneHourAgo;
        </script>
    </body>
</html>

Output:

Moment {'14 Dec 2022 21:00'}
1671018826058
false

As you can see, this approach produces the same result as the first one. However, you must work with a moment date object. If you don’t want to subtract 3600*1000 because, you don’t understand that is 1 hour ago (converted in milliseconds). Then, you can use the subtract() method of moment library to return the timestamp of 1 hour ago:

<html>
    <body>
        <p>Result</p>
        <p>Result</p>
        <p>Result</p>
        <script src="https://momentjs.com/downloads/moment.js"></script>
        <script type="text/javascript">
            let results = document.getElementsByTagName("p");

            // Create a Date object of 9 pm 14th December 2022
            let myDate = moment("14 Dec 2022 21:00");
            results[0].innerHTML = myDate;

            // Get the timestamp of 1 hour ago in milliseconds
            let oneHourAgo = moment().subtract(1, "hours");
            results[1].innerHTML = oneHourAgo;

            // Check if myDate is less than 1 hour ago
            results[2].innerHTML = myDate < oneHourAgo;
        </script>
    </body>
</html>

Output:

Moment {'14 Dec 2022 21:00'}
Moment {'Wed Dec 14 2022 18:53:46'}
false

As you can see, the time stamp of one hour ago is not a UNIX timestamp anymore. It has been converted to Moment type because we have used the subtract() method to get the moment of 1 hour ago.

Summary

This article has answered how to check if a Date is less than 1 hour ago using JavaScript. You should use the first method if you know the logic behind the calculations. We hope the information in this tutorial will be helpful to you. If you have any questions, feel free to leave a reply, we will answer soon. 

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *