使用Moment输出HighCharts的Date.UTC()对象? [英] Using Moment to output Date.UTC() object for HighCharts?

查看:534
本文介绍了使用Moment输出HighCharts的Date.UTC()对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Highcharts可以在本机上使用Unix Offset时间,但是通过Date对象更可读:

  Date .UTC(2003,8,25)

Moment.js有没有办法输出这个确切的对象?

  var momentDate = moment.utc([2003,08,25]); 
var JSDate = momentDate.toDate();
//不知道去哪里实际输出Date.UTC(2003,8,25)


解决方案

我认为对于 Date.UTC 的功能可能会有一些混淆。



Date.UTC() 不返回Date对象。它根据世界时间返回指定日期和1970年1月1日午夜之间的毫秒数。这正是Highcharts想要的。正如你所建议的,它比自己编写的毫秒数更加人性化。例如:

  var d = Date.UTC(2012,02,30); 
// d持有值1333065600000

Moment.js中类似的功能可以通过 valueOf() 方法:

  var d = moment.utc([2012,02,30])。valueOf(); 
// d持有值1333065600000


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)

解决方案

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

这篇关于使用Moment输出HighCharts的Date.UTC()对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆