1

Consider the following two snippets in a browser's JavaScript console (I have tried with Google Chrome)

1st Statement:

s = "2014-03-03 18:30:00";
d = new Date(s);
// Mon Mar 03 2014 18:30:00 GMT+0100 (CET)

2nd Statement:

s = "2014-03-03T18:30:00";
d = new Date(s);
// Mon Mar 03 2014 19:30:00 GMT+0100 (CET)

See? The parsed date and time differs with one hour for me, as I live in UTC+1.

But why does the JavaScript Date object parse these two strings differently, for there is no time zone given at all?

1
  • 2
    The 1st one is parsed as local time, the 2nd as UTC. When you the T in the 2nd example, that means you are specifying a time and a time zone. Since you don't give it a timezone, it assumes it as 0 (UTC). Commented Mar 3, 2014 at 16:16

1 Answer 1

2
  • The 2014-03-03T... notation is a fancy JavaScript Date Time String Format and expects a time zone. If you don't provide one, it defaults to Z (UTC).

  • The 2014-03-03 18:30:00 notation, however, is just a regular string without an interesting name and, if you don't provide a time zone, it assumes local time.

This info was taken from the MDN article about Date.parse().

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

Comments

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.