Skip to content Skip to sidebar Skip to footer

Javascript Date Object Returning December 31st 1969

If you are using a date in the form of milliseconds does it need to be converted to a string in order for the Date object to recognize it? 'values':[ {x:1390636800000 , y:12} ,

Solution 1:

If you are using a date in the form of milliseconds does it need to be converted to a string in order for the Date object to recognize it?

No, the value must be a Number (e.g. 1390636800000 not "1390636800000"). If a string is provided, Date will attempt to parse it.

Where a time value is provided to the Date constructor, it is assumed to be milliseconds since 1970-01-01T00:00:00Z. That is, UTC.

So if you are in a timezone of say UTC -5:00, then

newDate(0);

will return an object with a local date and time of 1969-12-31T19:00:00UTC-05:00

Solution 2:

Date doesn't need the number of millisecounds to be cast as a string:

> newDate(1390651200000)
SatJan25201407:00:00GMT-0500 (EST)

To print the time in the format you're looking for, you need to change the specifier string:

> d3.time.format('%I%p')(newDate(1390640400000))
"04AM"
> d3.time.format('%B/%d/%Y')(newDate(1390640400000))
"January/25/2014"

Post a Comment for "Javascript Date Object Returning December 31st 1969"