To check whether a given date occurred less than one hour ago, we can compare its timestamp with the current time. Since JavaScript represents dates internally in milliseconds, one hour can be converted into milliseconds and used for the comparison.
The following approaches can be used:
- Calculate the difference between the current date and the given date in milliseconds.
- Compare the difference with the number of milliseconds in one hour.
- Use Date.now() to directly calculate the timestamp for one hour ago.
Approach 1: Using Date Difference
Calculate the difference between the current date and the given date. If the difference is less than one hour, the date occurred within the last hour.
Example: This example checks whether the previous date is less than one hour ago.
<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>
- d - prev_date calculates the difference between the two dates in milliseconds.
- ONE_HOUR stores the number of milliseconds in one hour.
- If the difference is less than ONE_HOUR, the date is considered less than one hour ago.
Approach 2: Using Date.now()
Calculate the timestamp for one hour ago by subtracting one hour in milliseconds from the current timestamp. Then compare the given date with this timestamp.
Example: This example uses Date.now() to determine whether the given date occurred within the last hour.
<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.getTime());
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>