2016-12-30 69 views
0

我正在编写node.js上的代码,我需要设置小时,分钟,秒到现在的日期而不更改时区。 (我从客户端的时间,将其传送时间为hh:mm:由UTC时区ss格式)设置时区没有更改

我在设置的时间码是:

var now = new Date(); 

    console.log(now); 
    now.setHours(format.h); 
    now.setMinutes(format.m); 
    now.setSeconds(format.s); 

现在时间是:

2016-12-30T13:30:17.586Z 

和格式为:

13:29:29 

当我设置秒,其结果是

2016-12-30T18:29:29.345Z 

看来时区在变化;如何在没有时区更改的情况下设置小时?

  • UPDATE

我安装了momentjs这里是我的代码:

var now = moment(); 
    console.log("before: " + now.format()); 
    now.add(format.h, 'hours'); 
    now.add(format.m, 'minutes'); 
    now.add(format.s, 'seconds'); 
    console.log("after: " + now.format()); 

这里是日志:

format time= 3:45:38 
before: 2017-01-06T12:55:45+03:30 
after: 2017-01-06T16:41:23+03:30 

其实时间应该2017-01-06T15:45:38+00:00

+0

难道[我的回答(https://stackoverflow.com/questions/41397226/sethour-without-change-on-time-zone/41397636#41397636)以下的帮助吗?如果是这样,那么你可以考虑[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)。 – rsp

回答

0

对于任何复杂的日期/时间的操作,最好使用moment

moment().set('hour', 13); 

它将为您节省很多麻烦。

请参见:http://momentjs.com/docs/