Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
moment('2013-10-06').valueOf() and Date.parse('2013-10-06') do not result in the same time value if you are not in UTC time zone (I'm in Tokyo, +09:00).
A string like 'YYYY-MM-DD' (date only) is one of the valid ISO8601 format, so moment is expected to set it in UTC time zone, but actually moment seems to set it in local time zone.
The following example shows that you can see the different time value between moment and native Date.parse output if you are not in UTC time zone.
I don't know of anything in the spec specifying that date strings are expected to be in UTC. From Wikipedia's entry on ISO 8601:
That doesn't actually say what time a date should be interpreted as (dates, as far as 8601 is concerned, are just dates, not a specific number of milliseconds), but given that we are translating a date to a time, it seems reasonable to follow the time rules, where everything is local unless specified otherwise. So I believe the behavior you see is correct. @mj1856, can you confirm? I don't have a copy of the spec and I'm certainly no expert on it.
If you want to explicitly specify UTC, use
moment.utc('2013-10-06').format(); //=> "2013-10-06T00:00:00+00:00"
Alternatively, can also use
var m = moment('2013-10-06Z'); m.format(); // => "2013-10-05T20:00:00-04:00" m.utc().format(); //=> "2013-10-06T00:00:00+00:00"
Finally, you can of course pass the
moment(Date.parse('2013-10-06')).format(); //=> "2013-10-05T20:00:00-04:00"
I hope that helps!
Hmm, This topic seems to be more complicated than I expected.
ECMA-262 says the following at 22.214.171.124 [http://www.ecma-international.org/publications/standards/Ecma-262.htm]
I guess a date-only string without time zone follow this rule, and some modern browsers (Chrome, Firefox) look doing so. (although I'm also not sure what is the right spec..)
Your alternatives looks great :)
There is a bit of a mismatch because ISO8601 implies that any time that doesn't have a
In reality, browsers that support the ISO format tend to follow the ES5 rule when provided an input in
One of the primary reasons for using moment (IMHO) is to have uniformity in parsing. Browser specifics and ES5 specs are not something we should aim for feature parity with. Moment is more than just a shim. One could argue that ES5 is "doing it wrong", and that moment implements ISO8601 more precisely. Although ES5 does make it clear that theirs is a "based upon a simplification of the ISO 8601 Extended Format", so it doesn't have to precisely implement it.
Regarding moment's way of allowing the
In general, I'd say moment's implementation is about as good as it's going to get.
Thanks for the clarifications.
I was just wondering that the other day: "I wonder if Matt's precise date library will support dates as dates and not as times at midnight?" Would clear up some of the ISO-vs-browser differences here and would help with other issues like midnight DSTs.
It sure will. I'm modeling much of it off of the Noda Time core types, so it would be called a
Just need more time in my day. Too busy with other things to gain momentum on this presently.