2

I try to get UTC Date. Why

(new Date("2015-04-22")).getUTCDate() = 22

And

(new Date("Apr 22, 2015")).getUTCDate() = 21

?

7
  • I get the result of 22 Commented Apr 22, 2015 at 13:25
  • try this one: Date.UTC(2015-04-22); Commented Apr 22, 2015 at 13:26
  • Depend of the local time, see : w3schools.com/jsref/jsref_getutcdate.asp Commented Apr 22, 2015 at 13:26
  • @InformatikBabo You want to check 1989? Commented Apr 22, 2015 at 13:27
  • 1
    @user3629171 No, I think 2015-04-22 is an universal format. But Apr 22, 2015 is a local format. Commented Apr 22, 2015 at 13:30

1 Answer 1

2

To quote MDN on the workings of Date.parse (called by new Date("string")):

Differences in assumed time zone

Given a date string of "March 7, 2014", parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC. Therefore Date objects produced using those strings will represent different moments in time unless the system is set with a local time zone of UTC. This means that two date strings that appear equivalent may result in two different values depending on the format of the string that is being converted (this behavior is changed in ECMAScript ed 6 so that both will be treated as local).

Watch out for the warning I've highlighted on the last line

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

1 Comment

Indeed, I was about to quote this from MDN dateString A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected).

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.