2016-12-06 34 views
0
var end, moment, timeFormat; 
moment = require('moment'); 
end = moment.utc('2016-11-29T23:59:59.999'); 
console.dir(end.format()); 
timeFormat = 'YYYY-MM-DDThh:mm:ss.SSS'; 
console.dir(end.format(timeFormat)); 

輸出:如何獲取moment.js以毫秒爲單位輸出UTC時間的格式化時間?

'2016-11-29T23:59:59Z' 
'2016-11-29T11:59:59.999' 

我真正需要的:

'2016-11-29T23:59:59.999' 

爲什麼會出現這些2個輸出之間有12小時的時差?我可以只增加12個小時,但這是hacky。我不明白爲什麼在我給它一個格式之後,突然從我的約會中減去12個小時。似乎不太可能這是一個錯誤;我更可能誤解Moment正在做的事情。

我的時區是格林威治標準時間+8。

+0

因爲你正在使用'hh'(0-12),而不是'HH'(0-23),參見['格式( )'](http://momentjs.com/docs/#/displaying/format/)文檔。 – VincenzoC

回答

3

由於您使用hh(01-12),而不是HH(00-23);請參閱Moment.js format()文檔。

這裏是一個工作示例:

var end, wrongTimeFormat, timeFormat; 
 
end = moment.utc('2016-11-29T23:59:59.999'); 
 
console.dir(end.format()); 
 
wrongTimeFormat = 'YYYY-MM-DDThh:mm:ss.SSS'; 
 
timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS'; 
 
console.dir(end.format(wrongTimeFormat)); 
 
console.dir(end.format(timeFormat));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.0/moment.min.js"></script>

+1

我知道這是一件小事。啊。謝謝。 – jcollum

1

使用大寫HH

timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS';

2

According to the Moment.js docs,小寫hh會產生在01-12範圍小時,這是指與AM/PM一起使用。你需要資金HH(00-23 「軍用時間」),在

timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS' 
相關問題