moment().toNow();
moment().toNow(Boolean);
显示时间的常用方法是通过 moment#toNow
处理。
有时称为时间间隔或相对时间。
这类似于 moment.fromNow
,但给出相反的间隔:a.fromNow() = - a.toNow()
。
这类似于 moment.to
,但在当前时间有特殊情况。
如果要控制间隔的两个端点,则使用 moment.to
。
moment([2007, 0, 29]).toNow(); // 4 年内
如果传入 true
,则可以获取不带前缀的值。
moment([2007, 0, 29]).toNow(); // 4 年内
moment([2007, 0, 29]).toNow(true); // 4 年
基本的字符串由当前的语言环境自定义。
下表概述了每个时间长度显示的字符串的细分。
范围 | 键 | 样本输出 |
---|---|---|
0 至 44 秒 | s | 几秒内 |
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 对象无效,则结果为本地化的无效日期字符串。
moment().toNow();
moment().toNow(Boolean);
A common way of displaying time is handled by moment#toNow
. This is sometimes called timeago or relative time.
This is similar to moment.fromNow
, but gives
the opposite interval: a.fromNow() = - a.toNow()
.
This is similar to moment.to
, but is special-cased
for the current time. Use moment.to
, if you want to control the two end
points of the interval.
moment([2007, 0, 29]).toNow(); // in 4 years
If you pass true
, you can get the value without the prefix.
moment([2007, 0, 29]).toNow(); // in 4 years
moment([2007, 0, 29]).toNow(true); // 4 years
The base strings are customized by the current locale.
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 | in seconds |
45 to 89 seconds | m | in a minute |
90 seconds to 44 minutes | mm | in 2 minutes ... in 44 minutes |
45 to 89 minutes | h | in an hour |
90 minutes to 21 hours | hh | in 2 hours ... in 21 hours |
22 to 35 hours | d | in a day |
36 hours to 25 days | dd | in 2 days ... in 25 days |
26 to 45 days | M | in a month |
45 to 319 days | MM | in 2 months ... in 10 months |
320 to 547 days (1.5 years) | y | in a year |
548 days+ | yy | in 2 years ... in 20 years |
From version 2.10.3, if the target moment object is invalid the result is the localized Invalid date string.