Here's a simple JavaScript function I have:
function checkTime() {
var current = new Date();
var target = new Date('April 10, 2017 12:11:00');
if (current < target) {
$('#modalnew').modal('show');
} else {
window.location.replace('https://www.example.php');
}
}
The function works as expected, but the date comparison logic is puzzling me. Despite looking at similar examples online, I'm still unable to grasp it fully. For instance, consider this hypothetical situation without any code:
Current Time (now) = April 10, 2017 12:22:00
Target Time (date set) = April 10, 2017 12:11:00
Given that current
seems to be 11 minutes ahead of target
, why does the function trigger the window.location
? It appears illogical based on the time values alone. What exactly is being compared in this scenario?