0

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).

Kranthi
  • 1,377
  • 1
  • 17
  • 34

1 Answers1

2

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()
marekful
  • 14,986
  • 6
  • 37
  • 59
  • thanks for the answer. can't I convert the result to date object? I mean i want result of toUTCString() as date object. – Kranthi Dec 27 '14 at 11:58
  • Your date object's internal datetime pointer is at `Sat, 03 Feb 2001 00:00:00 GMT`. There's nothing to convert. It will always be represented according to timezone unless you specify not to. – marekful Dec 27 '14 at 12:00