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

2
  • which browser you are using ? Commented Dec 27, 2014 at 11:39
  • Mine is chrome browser Commented Dec 27, 2014 at 11:59

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer. can't I convert the result to date object? I mean i want result of toUTCString() as date object.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.