有时,只想要 moment#from 的所有优点,但又不想创建两个 moment,而只想显示一段时长。

使用 moment.duration().humanize()

moment.duration(1, "minutes").humanize(); // 1 分钟
moment.duration(2, "minutes").humanize(); // 2 分钟
moment.duration(24, "hours").humanize();  // 1 天

默认情况下,返回的字符串是没有后缀。 如果需要后缀,则按如下所示传入 true。

moment.duration(1, "minutes").humanize(true); // 1 分钟内


moment.duration(-1, "minutes").humanize(true); // 1 分钟前

无效的时长会被人性化为 Invalid Date 的本地化版本。

moment.duration.invalid().humanize(); // Invalid Date

Sometimes, you want all the goodness of moment#from but you don't want to have to create two moments, you just want to display a length of time.

Enter moment.duration().humanize().

moment.duration(1, "minutes").humanize(); // a minute
moment.duration(2, "minutes").humanize(); // 2 minutes
moment.duration(24, "hours").humanize();  // a day

By default, the return string is suffixless. If you want a suffix, pass in true as seen below.

moment.duration(1, "minutes").humanize(true); // in a minute

For suffixes before now, pass in a negative number.

moment.duration(-1, "minutes").humanize(true); // a minute ago

Invalid durations are humanized to the localized version of Invalid Date.

moment.duration.invalid().humanize(); // Invalid Date