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

寵の児 提交于 2019-12-13 18:32:07

问题


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)

回答1:


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


来源:https://stackoverflow.com/questions/27331225/using-moment-to-output-date-utc-object-for-highcharts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!