As JSON2.0.0+
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"}'
当前内容版权归 momentjs.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 momentjs.com .