How To Check If Date is Older Than 1 Hour in JavaScript?
Sep 23, 2021 . Admin
Hello Friends,
Now let's see example of how to check if date is older than 1 hour example. We will use how to check if date is older than 1 hour in javascript. Here you will learn how to use javascript check if date is older than 1 hour. This is a short guide on check if date is older than 1 hour. Let's get started with how to check if date is older than 1 hour in javascript.
Here i will give you many example how you can check if date is older than 1 hour javascript.
Example : 1<!DOCTYPE HTML> <html> <head> <title>How to check if date is older than 1 hour using JavaScript? - MyWebtuts.com </title> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/ bootstrap.min.css"> </head> <body class="bg-dark"> <div class="container mt-5"> <div class="row"> <div class="col-md-6 offset-md-3"> <div class="card"> <div class="card-header"> <h5>JavaScript check if date is older than 1 hour</h5> </div> <div class="card-body"> <p id = "emp1" ></p> <button onclick = "cmd()" class="btn btn-primary mb-2">Check</button> <p id = "emp2"></p> </div> </div> </div> </div> </div> <script> var cmp1 = document.getElementById("emp1"); var cmp2 = document.getElementById("emp2"); var bfr_date = new Date(); var d = new Date(); const check = (date) => { const HOUR = 1000 * 60 * 60; const anHourAgo = Date.now() - HOUR; return date > anHourAgo; } cmp1.innerHTML = "Previous date = " + bfr_date; function cmd() { var c = check(bfr_date); if (c) { cmp2.innerHTML = "Date is less " + "than 1 hour ago."; } else { cmp2.innerHTML = "Date is not less" + " than 1 hour ago."; } } </script> </body> </html>Output :
Date is less than 1 hour ago.Example : 2
<!DOCTYPE HTML> <html> <head> <title>How to check if date is older than 1 hour using JavaScript? - MyWebtuts.com </title> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/ bootstrap.min.css"> </head> <body class="bg-dark"> <div class="container mt-5"> <div class="row"> <div class="col-md-6 offset-md-3"> <div class="card"> <div class="card-header"> <h5>JavaScript check if date is older than 1 hour</h5> </div> <div class="card-body"> <p id = "emp1"></p> <button onclick = "cmd()" class="btn btn-primary mb-2">Check</button> <p id = "emp2"></p> </div> </div> </div> </div> </div> <script> var cmp1 = document.getElementById("emp1"); var cmp2 = document.getElementById("emp2"); 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); cmp1.innerHTML = "Previous date = " + prev_date; function cmd() { var ONE_HOUR = 60 * 60 * 1000; if ((d - prev_date) < ONE_HOUR) { cmp2.innerHTML = "Date is less" + " than 1 hour ago."; } else { cmp2.innerHTML = "Date is not " + "less than 1 hour ago."; } } </script> </body> </html>Output :
Date is not less than 1 hour ago.
It will help you...