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.

3

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Notice that == doesn't work on Date objects as you would expect
@Bergi Thanks for pointing me on that strange JS behaviour!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.