在瞬间格式化日期.js给出 1/1/2016

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

本文关键字:2016 给出 js 瞬间 格式化 日期      更新时间:2023-09-26
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;
    });
}

thingDate2016-04-30T00:00:00的(根据调试器(,但是当我格式化它时,fThingDate变得1/1/2016。为什么要到今年的1号?我该如何阻止它?

当日期时间是格式的字符串时,momentjs 似乎有一个问题:2016-04-30T00:00:00 .我在IE 10和Windows Server 2012 Std中运行了这个。

但这似乎解决了它:

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;
    });
}

注意:在将其归咎于IE之前,似乎有些人过去也遇到过同样的问题。