Kranthi
Kranthi

Reputation: 1417

javascript date issue with timezone

I'm converting a string date to date object but getting one day less. I googled but couldn't understand how to change get proper output. Can anyone help me or give me a reference where I can understand from.

new Date("2001-02-03")

browser resut: Fri Feb 02 2001 19:00:00 GMT-0500 (EST).

expected: Fri Feb 03 2001 19:00:00 GMT-0500 (EST).

Upvotes: 0

Views: 76

Answers (1)

marekful
marekful

Reputation: 15351

The browser represents JS dates with the system's timezone taken into account. The given date string has no time portion so it assumes 00:00:00 for time. You appear to be in the -05:00 timezone, so the date will be represented five hours behind your specified time which is 7pm the previous day. You can use toUTCString() to see the date information w/o timezone.

var d = new Date("2001-02-03");
d.toUTCString()
"Sat, 03 Feb 2001 00:00:00 GMT"

or in a shorter form

(new Date("2001-02-03")).toUTCString()

Upvotes: 2

Related Questions