fromNow()


moment().fromNow();
moment().fromNow(Boolean);

显示时间的常用方法是通过 moment#fromNow 处理。 有时称为时间间隔或相对时间。

moment([2007, 0, 29]).fromNow(); // 4 年前

If you pass true, you can get the value without the suffix. 如果传入 true,则可以获得不带后缀的值。

moment([2007, 0, 29]).fromNow();     // 4 年前
moment([2007, 0, 29]).fromNow(true); // 4 年

基本的字符串由当前的语言环境自定义。 时间会舍入到最接近的秒数。

下表概述了每个时间长度显示的字符串的细分。

范围 样本输出
0 至 44 秒 s 几秒前
未设定 ss 44 秒前
45 至 89 秒 m 1 分钟前
90 秒至 44 分钟 mm 2 分钟前 ... 44 分钟前
45 至 89 分钟 h 1 小时前
90 分钟至 21 小时 hh 2 小时前 ... 21 小时前
22 至 35 小时 d 1 天前
36 小时至 25 天 dd 2 天前 ... 25 天前
26 至 45 天 M 1 个月前
45 至 319 天 MM 2 个月前 ... 10 个月前
320 至 547 天 (1.5 年) y 1 年前
548 天+ yy 2 年前 ... 20 年前

注意:从 2.10.3 版本开始,如果目标 moment 对象无效,则结果为本地化的无效日期字符串。

注意:ss 键新增于 2.18.0。 这是一个可选的阈值。 除非用户手动设置 ss 阈值,否则它将永远不会显示。 在设置 ss 阈值之前,它默认为 s 阈值减去 1(因此对用户不可见)的值。

moment().fromNow();
moment().fromNow(Boolean);

A common way of displaying time is handled by moment#fromNow. This is sometimes called timeago or relative time.

moment([2007, 0, 29]).fromNow(); // 4 years ago

If you pass true, you can get the value without the suffix.

moment([2007, 0, 29]).fromNow();     // 4 years ago
moment([2007, 0, 29]).fromNow(true); // 4 years

The base strings are customized by the current locale. Time is rounded to the nearest second.

The breakdown of which string is displayed for each length of time is outlined in the table below.

Range Key Sample Output
0 to 44 seconds s a few seconds ago
unset ss 44 seconds ago
45 to 89 seconds m a minute ago
90 seconds to 44 minutes mm 2 minutes ago ... 44 minutes ago
45 to 89 minutes h an hour ago
90 minutes to 21 hours hh 2 hours ago ... 21 hours ago
22 to 35 hours d a day ago
36 hours to 25 days dd 2 days ago ... 25 days ago
26 to 45 days M a month ago
45 to 319 days MM 2 months ago ... 10 months ago
320 to 547 days (1.5 years) y a year ago
548 days+ yy 2 years ago ... 20 years ago

Note: From version 2.10.3, if the target moment object is invalid the result is the localized Invalid date string.

Note: The ss key was added in 2.18.0. It is an optional threshold. It will never display UNLESS the user manually sets the ss threshold. Until the ss threshold is set, it defaults to the value of the s threshold minus 1 (so, invisible to the user).