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 45 seconds | s | in seconds |
| 45 to 90 seconds | m | in a minute |
| 90 seconds to 45 minutes | mm | in 2 minutes ... in 45 minutes |
| 45 to 90 minutes | h | in an hour |
| 90 minutes to 22 hours | hh | in 2 hours ... in 22 hours |
| 22 to 36 hours | d | in a day |
| 36 hours to 25 days | dd | in 2 days ... in 25 days |
| 25 to 45 days | M | in a month |
| 45 to 345 days | MM | in 2 months ... in 11 months |
| 345 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.