2

I have this simple js function:

function checkTime() {  
  var d1 = new Date();
  var d2 = new Date('April 10, 2017 12:11:00');

  if (d1 < d2) {
    $('#modalnew').modal('show');
  } else {
    window.location.replace('https://www.example.php');
  }
}

It works fine, but I don't understand the date comparison. This may be a "dumb" question, but I can't find answers on google. F.E.: - no code, just an example -

d1 (now) = April 10, 2017 12:22:00
d2 (date set) = April 10, 2017 12:11:00

Why is the d1 less than d2 and activates the window.location? In a logical order d1 it is 11 minutes greater than d2. On which parameter does it exactly compare?

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
CodeNewb
  • 35
  • 4
  • This might help: http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Rajesh Apr 10 '17 at 10:29
  • 1
    d1 < d2 is false that is why the else part is fired so technically di is not less than d2 its executing right isn't it? – Vinod Louis Apr 10 '17 at 10:34
  • 1
    Voting to close because OPs assumption is clearly wrong and does not even need an answer. – Salman A Apr 10 '17 at 12:54

3 Answers3

2

In your case d1 is not less but more than d2 which is the expected result and hence the window.location.replace is executed .

While comparing the date it evaluates to if (d1.valueOf()< d2.valueOf()) . valueOf() delivers time in milliseconds since beginning of 00:00:00 UTC Thursday 1, January 1970 and then compares it.

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
2

To compare two dates you better use Date.prototype.getTime() method that returns the numeric value corresponding to the time for the specified date according to universal time.

The result number for each date d1 and d2 will explain better the short and bigger date.

Code:

function checkTime() {  
  var d1 = new Date();
  var d2 = new Date('April 10, 2017 12:11:00');

  d1.getTime() < d2.getTime()
    ? $('#modalnew').modal('show')
    : window.location.replace('https://www.example.php');
}
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • 1
    No, wrong. You can compare dates using `<` and `>`. it is the `==` and `===` that cause problem (always return false even if dates are same). – Salman A Apr 10 '17 at 12:48
  • @SalmanA check that [Date.prototype.getTime()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime) method returns the numeric value corresponding to the time for the specified date according to universal time. So, you can compare numeric values with `<` and `>` – Yosvel Quintero Apr 19 '17 at 06:55
  • I am saying is that two `Date`s can be compared directly using `<` and `>` (and `<=` and `>=`). These operators will call `.valueOf()` method behind the scenes which, for dates, returns same value as `.getTime()`. OPs code is correct and your answer is redundant. – Salman A Apr 19 '17 at 07:11
-1

Compare two dates in JavaScript Convert your time using getTime() and then compare More Detail ,Detail, Working Demo

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Muhammad Usman
  • 345
  • 1
  • 11