MomentJS - Time to X


Advertisements

This method will help to display moment as per in relation with another moment instead of current one.

Syntax

moment().to(Moment|String|Number|Date|Array);
moment().to(Moment|String|Number|Date|Array, Boolean);

Example

var a = moment([1995, 3, 15]);
var b = moment([1998, 10, 01]);
var c = a.to(b);

OR

Example

var a = moment([1995, 3, 15]).to(moment([1998, 10, 01]));

Output

Time Moment

Example 2

var a = moment([1995, 3, 15]).to(new Date());

Output

Time New Date

Example 3

var a = moment([2018, 10, 11]).to(moment[2015, 10, 11], true);

Output

Time True

If you want to remove the prefix in or ago in the display, use true.

momentjs_formatting_date_and_time.htm
Advertisements