toJSON()


moment().toJSON();

当将对象序列化为 JSON 时,如果有 Moment 对象,则它会将被解释为 ISO8601 字符串,并调整到 UTC。

JSON.stringify({
    postDate : moment()
}); // '{"postDate":"2013-02-04T22:44:30.652Z"}'

如果希望使用一个 ISO8601 字符串来反映该 moment 的 utcOffset(),则可以像这样修改 toJSON 函数:

moment.fn.toJSON = function() { return this.format(); }

这会更改行为,如下所示:

JSON.stringify({
    postDate : moment()
}); // '{"postDate":"2013-02-04T14:44:30-08:00"}'
moment().toJSON();

When serializing an object to JSON, if there is a Moment object, it will be represented as an ISO8601 string, adjusted to UTC.

JSON.stringify({
    postDate : moment()
}); // '{"postDate":"2013-02-04T22:44:30.652Z"}'

If instead you would like an ISO8601 string that reflects the moment's utcOffset(), then you can modify the toJSON function like this:

moment.fn.toJSON = function() { return this.format(); }

This changes the behavior as follows:

JSON.stringify({
    postDate : moment()
}); // '{"postDate":"2013-02-04T14:44:30-08:00"}'