mrkskwsnck
mrkskwsnck

Reputation: 357

JavaScript ISO 8601 string into Date object

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?

Upvotes: 1

Views: 2454

Answers (1)

Álvaro González
Álvaro González

Reputation: 146340

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

Upvotes: 2

Related Questions