Moment.js:将持续时间细分为年,月,周,天等

时间:2018-09-28 15:32:16

标签: javascript momentjs

时刻能提供这种便利吗?

如果我有两个日期,我能否从一个可以解构以下信息的时刻得到一个对象:

<body onload="functionToBeExecutedAfterPageLoads();">

1 个答案:

答案 0 :(得分:1)

是的,您可以使用durations。像这样:

const theDiffObject = {
    years: moment.duration(timeDiff).years(),
    months: moment.duration(timeDiff).months(),
    weeks: moment.duration(timeDiff).weeks(),
    days: moment.duration(timeDiff).days()
}