How to check if date is less than 1 hour ago using JavaScript ? Last Updated : 22 Dec, 2022 Comments Improve Suggest changes 2 Likes Like Report Given a date and the task is to check whether the given date is less than 1 hour ago or not with the help of JavaScript. Approach 1: Count the milliseconds of the difference between the current and prev_date.If those are greater than milliseconds in 1 hour, then it returns false otherwise returns true. Example: This example implements the above approach. html <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var prev_date = new Date(); var d = new Date(); Date.prototype.addHours = function(h) { this.setTime(this.getTime() + (h*60*60*1000)); return this; } prev_date.addHours(-2); el_up.innerHTML = "Click on the button to " + "check if the date is less than " + "1 hour ago.<br>Previous date = " + prev_date; function gfg_Run() { // Hour in milliseconds var ONE_HOUR = 60 * 60 * 1000; if ((d - prev_date) < ONE_HOUR) { el_down.innerHTML = "Date is less" + " than 1 hour ago."; } else { el_down.innerHTML = "Date is not " + "less than 1 hour ago."; } } </script> Output: Approach 2: Subtract 1 hour milliseconds from current time.Compare the milliseconds of the current and prev_date.If those are greater than milliseconds in 1 hour, then it return false otherwise returns true. Example 2: This example uses the approach as discussed above. html <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var prev_date = new Date(); var d = new Date(); const check = (date) => { const HOUR = 1000 * 60 * 60; const anHourAgo = Date.now() - HOUR; return date > anHourAgo; } el_up.innerHTML = "Click on the button to check" + " if the date is less than 1 hour" + " ago.<br>Previous date = " + prev_date; function gfg_Run() { var c = check(prev_date); if (c) { el_down.innerHTML = "Date is less " + "than 1 hour ago."; } else { el_down.innerHTML = "Date is not less" + " than 1 hour ago."; } } </script> Output: Create Quiz Comment P PranchalKatiyar Follow 2 Improve P PranchalKatiyar Follow 2 Improve Article Tags : JavaScript Web Technologies javascript-date JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like