moment(). toobject()输出格式

moment().toObject() output format

本文关键字:格式 输出 toobject moment      更新时间:2023-09-26

我正在使用MomentJS和目前的moment().toObject()输出

{
years:2016,
months:10,
date:3,
hours:18,
milliseconds:85,
minutes:26,
seconds:26
}

如何将输出格式更改为

{
year: 2016, //without 's'
month: 10, //without 's'
day: 3, // instead of date
...
}

新建一个对象:

var a = moment().toObject();
var b = { year: a.years, month: a.months + 1, day: a.date };

注意,我增加了月份,以考虑时刻输出月份0-11而不是1-12。你可能不喜欢,也可能不喜欢,但我认为在这里指出这一点可能是有用的。