fuzzybabybunny
fuzzybabybunny

Reputation: 5254

Using Moment to output Date.UTC() object for HighCharts?

I know that Highcharts can take Unix Offset time natively, but it's more readable to pass it a Date object:

Date.UTC(2003,8,25)

Is there any way for Moment.js to output this exact object?

var momentDate = moment.utc([2003, 08, 25]);
var JSDate = momentDate.toDate();
//Not sure where to go to actually output Date.UTC(2003,8,25)

Upvotes: 1

Views: 4279

Answers (1)

Halvor Holsten Strand
Halvor Holsten Strand

Reputation: 20536

I think there may be some confusion as to the functionality of Date.UTC.

Date.UTC() does not return a Date object. It returns the number of milliseconds between a specified date and midnight of January 1, 1970, according to universal time. This is exactly what Highcharts wants. As you suggest, it is way more human-readable than typing the number of milliseconds itself. For example:

var d = Date.UTC(2012,02,30);
// d holds the value 1333065600000

Similar functionality in Moment.js can be achieved with the valueOf() method:

var d = moment.utc([2012,02,30]).valueOf();
// d holds the value 1333065600000

Upvotes: 12

Related Questions