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));
输出:
'2016-11-29T23:59:59Z'
'2016-11-29T11:59:59.999'
我真正需要的是:
'2016-11-29T23:59:59.999'
为什么这两个输出之间有12小时的差异?我可以添加12小时,但这很黑。我不明白为什么一旦我给它一个格式,我的约会时刻突然减去了12个小时。似乎不太可能这是一个错误;我更可能误解了Moment正在做的事情。
我的时区是GMT + 8。
答案 0 :(得分: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 :(得分:2)
According to the Moment.js docs,小写hh
将在01-12范围内产生小时数,这意味着与am / pm一起使用。你需要资金HH
(00-23,“军事时间”),如
timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS'
答案 2 :(得分:1)
使用大写HH
timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS';