2016-12-30 32 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/