"getDate().toJSON()" 失去了一天

"getDate().toJSON()" loses a day

我正在使用 pikaday date picker plugin (through an angular directive and with momentjs) 并将值发送到服务器。转换为 json 似乎失去了一天:

var d = myPikaObject.getDate();
console.log(d);              // Thu Apr 30 2015 00:00:00 GMT+0200 (SAST)
console.log(d.toJSON());     // 2015-04-29T22:00:00.000Z

我认为这是一个 momentjs 问题,但我不知道出了什么问题。

一切都与约会的格式有关。

当你只打印 d 时,你有这个:

Thu Apr 30 2015 00:00:00 GMT+0200 (SAST)

现在是 GMT +2,所以当您打印 d.ToJson() 时,您损失了 2 小时。所以你是前一天22pm

鉴于您已经有时间使用 momentjs,您可以尝试 moment.utc() 方法。文档说:

As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc()

moment('2012 juillet', 'YYYY MMM', 'fr');
moment('2012 July',    'YYYY MMM', 'en');

您可以使用 utc() 方法做更多事情。

moment.utc();
moment.utc(Number);
moment.utc(Number[]);
moment.utc(String);
moment.utc(String, String);
moment.utc(String, String[]);
moment.utc(String, String, String);
moment.utc(Moment);
moment.utc(Date);