Permalink
c7fe27b Nov 27, 2016
@ichernev @timrwood @mj1856
96 lines (86 sloc) 2.08 KB
title version signature
Time from now
1.0.0
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.

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
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

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