Javascript Date对象返回1969年12月31日 [英] Javascript Date object returning December 31st 1969

查看:194
本文介绍了Javascript Date对象返回1969年12月31日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您以毫秒的形式使用日期,则需要转换到字符串,以便Date对象能够识别它?

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} , 
           { x:1390640400000 , y:17} , 
           { x:1390644000000 , y:17}, 
           { x:1390647600000 , y:15}, 
           { x:1390651200000 , y:8} ]

如果是这样,我将如何进行转换

If so, how would i go about converting it so that it is used properly by

chart.xAxis
 .axisLabel("Time (s)")
 .tickFormat(function(d){return d3.time.format('%I%p')(new Date(d))});

并且没有吐出12月31日/ 1969年的日期?我试图stringify整个对象,但是没有工作。有什么建议么?非常感谢您的帮助,对不起,如果这是一个愚蠢的问题

and it doesn't spit out the December/31/1969 date? I tried to stringify the whole object but that didn't work. Any suggestions? Thanks for any help,and sorry if this is a silly question

(编辑)这是我使用的工作代码,不能正确显示时间

(edit) this is the working code i am using that doesn't correctly display the time

var chart;

nv.addGraph(function() {
  chart = nv.models.lineChart()
  .options({
   margin: {left: 100, bottom: 100},
   x: function(d,i) { return i},
   showXAxis: true,
   showYAxis: true,
   transitionDuration: 250
 })
 ;

 chart.xAxis
   .axisLabel("Time (s)")
   .tickFormat(function(d){return d3.time.format('%m/%d/%y')(new Date(d))});

 chart.yAxis
   .tickFormat(d3.format('d'))
   ;

 d3.select('#chart1 svg')
   .datum(data1())
   .call(chart);

 nv.utils.windowResize(chart.update);


 chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e));    });

 return chart;
});

function data1() {

 return  [
   {
     "values":[ {x:1390636800000 , y:12} , { x:1390640400000 , y:17} , { x:1390644000000 , y:17}, { x:1390647600000 , y:15}, { x:1390651200000 , y:8} ],
  "key": "First Dude "
},
{
  "values": [ { x:1390636800000 , y:16} , { x:1390640400000 , y:16} , { x:1390644000000 , y:16}, { x:1390647600000 , y:12}, { x:1390651200000 , y:5}],
  "key": "Second Dude "
},
{
  "values": [ { x:1390636800000 , y:5} , { x:1390640400000 , y:5} , { x:1390644000000 , y:5}, { x:1390647600000 , y:3}, { x:1390651200000 , y:1}],
  "key": "Third Dude "
}
,
{
  "values": [ { x:1390636800000 , y:8} , { x:1390640400000 , y:18} , { x:1390644000000 , y:18}, { x:1390647600000 , y:9}, { x:1390651200000 , y:7}],
  "key": "Fourth Dude "
}
 ];

}


推荐答案


如果你使用一个以毫秒为单位的日期,它需要转换为一个字符串,以便Date对象能够识别它吗?

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?

否,值必须是数字(例如1390636800000不是1390636800000)。如果提供了字符串,Date将尝试解析它。

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

其中时间值提供给Date构造函数,则假定自1970-01-01T00:00:00Z以来是毫秒。也就是UTC。

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.

所以如果你处于UTC -5:00的时区,那么

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

new Date(0);

将返回一个本地日期和时间为1969-12-31T19:00:00UTC- 05:00

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

这篇关于Javascript Date对象返回1969年12月31日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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