2016-12-06 20 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' 
相关问题