Reputation: 46589
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));
Output:
'2016-11-29T23:59:59Z'
'2016-11-29T11:59:59.999'
What I really need:
'2016-11-29T23:59:59.999'
Why is there a 12 hour difference between those 2 outputs? I could just add 12 hours to it, but that is hacky. I don't understand why moment is suddenly subtracting 12 hours from my date as soon as I give it a format. Seems unlikely this is a bug; more likely me misunderstanding what Moment is doing.
My timezone is GMT+8.
Upvotes: 1
Views: 533
Reputation: 31482
Because you are using hh
(01-12) instead of HH
(00-23); see the Moment.js format()
docs.
Here is a working example:
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>
Upvotes: 3
Reputation: 16777
According to the Moment.js docs, lowercase hh
will produce hours in the 01-12 range, which are meant to be used in conjunction with am/pm. You need capital HH
(00-23, "military time"), as in
timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS'
Upvotes: 2