- MomentJS Tutorial
- MomentJS - Home
- MomentJS - Overview
- MomentJS - Environment Setup
- MomentJS - Introduction
- MomentJS - Parsing Date And Time
- MomentJS - Date Validation
- MomentJS - Getter/Setter
- Manipulate Date And Time
- Formatting Date And Time
- MomentJS - Date Queries
- MomentJS - Internationalization
- MomentJS - Customization
- MomentJS - Durations
- MomentJS - Utilities
- MomentJS - Plugins
- MomentJS - Examples
- MomentJS Useful Resources
- MomentJS - Quick Guide
- MomentJS - Useful Resources
- MomentJS - Discussion
MomentJS - Time from now
This method will tell the length of time from now. Suppose for example if you pass a date to this method it will display the difference if it is in years, months, hours, minutes, or seconds.
Syntax
moment().fromNow(); moment().fromNow(Boolean);
Example
var changeddate = moment([2018, 0, 1]).fromNow();
Output
Observe that the date given to the moment is 01/01/2018, the difference till date is 4 months, so the output is given as 4 months ago.
Observe the following example to check the output when no inputs are given to the moment −
Example
var changeddate = moment().fromNow();
Output
Note that in the above shown output, it has the ago keyword appended at the end. In case you do not need it, pass true to fromNow(true) method as shown below −
Example
var changeddate = moment([2015, 10, 01]).fromNow(); var changeddate1 = moment([2015, 10, 01]).fromNow(true);
Output
momentjs_formatting_date_and_time.htm
Advertisements