8

When comparing dates in Javascript using <, >, =, >= and <= is the timezone used in any way in the comparison? I am hoping that the timezone is ignored.

Wolf
  • 9,679
  • 7
  • 62
  • 108
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • 1
    I think this question may help you: http://stackoverflow.com/questions/17545708/parse-date-without-timezone-javascript – sanyooh Oct 23 '14 at 15:22
  • There is no timezone in JavaScript dates. So *No*, it won't be used in the comparisons. – Bergi Dec 03 '14 at 11:57
  • The `=` operator you mention does not *compare* but *assign*. On the other hand, [`Date` objects, the operator `==` is not working as expected](http://stackoverflow.com/q/7606798/2932052). – Wolf Dec 03 '14 at 12:28

1 Answers1

10

The timezone part of string representation of a timestamp is taken into account as you would expect, when you convert it into JavaScript Date object: the internal value is a simple scalar, normalized to UTC. So there is no need for special timezone handling when comparing Date objects:

var d1 = new Date(Date.parse("Mon, 25 Dec 1995 13:30:00 +0430"));
var d2 = new Date(Date.parse("Mon, 25 Dec 1995 13:30:00 GMT"));
print("d1:", d1);
print("d2:", d2);
if (d1<d2) {
    print("d1 is less then d2");
} else if (d1>d2) {
    print("d1 is greater then d2");
} else {
    print("d1 equals to d2");
}

which gives this output:

d1: Mon Dec 25 1995 09:00:00 GMT+0000
d2: Mon Dec 25 1995 13:30:00 GMT+0000
d1 is less then d2

[see online demo]

You'll most probably get into trouble if you compare string representations of time stamps.

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • 2
    Notice that `==` doesn't work on `Date` objects as you would expect – Bergi Dec 03 '14 at 11:58
  • @Bergi Thanks for pointing me on that [strange JS behaviour](http://stackoverflow.com/q/7606798/2932052)! – Wolf Dec 03 '14 at 12:16