Da Best Da Best - 7 months ago 14
Javascript Question

Formatting a date in moment.js gives 1/1/2016

function getData(thing, thingDate) {
var fThingDate = moment(thingDate).format('MM/DD/YYYY');
return getAPIData('/api/things?thing=' + thing + '&thingDate=' + fThingDate).then(function (results) {
return results.data;
});
}


thingDate
is
2016-04-30T00:00:00
(according to the debugger) but when I format it,
fThingDate
becomes
1/1/2016
. Why is it going to the 1st of the year? And how do I stop it?

Answer

It seems like momentjs has an issue when the datetime is a string in the format: 2016-04-30T00:00:00. I ran this in IE 10 and Windows Server 2012 Std.

But this seemed to solve it:

function getData(thing, thingDate) {
    var fThingDate = moment(new Date(thingDate)).format('MM/DD/YYYY');
    return getAPIData('/api/things?thing=' + thing + '&thingDate=' + fThingDate).then(function (results) {
        return results.data;
    });
}

NOTE: Before blaming it on IE, it seems that a few people have ran into the same problem in the past as well.