Time to now2.10.3+
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 givesthe opposite interval: a.fromNow() = - a.toNow()
.
This is similar to moment.to
, but is special-casedfor the current time. Use moment.to
, if you want to control the two endpoints 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 isthe localized Invalid date string.
当前内容版权归 momentjs.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 momentjs.com .