How to subtract 2 dates on momentjs

You are correct, you can use moment's diff function to subtract two dates (see my example on Plunker):

var date1 = moment('2016-10-08 10:29:23');
var date2 = moment('2016-10-08 11:06:55');
var diff = date2.diff(date1);

Diff will be equal to 2252000, the number of milliseconds between the two date. See documentation for more details.

You can pass a second argument to diff with the measurement to use (years, months, weeks, days, hours, minutes, and seconds), so if you want to know the number of minutes between the two dates you can write:

var diffInMinutes = date2.diff(date1, 'minutes');

And you get 37 minutes.

Tags:

Momentjs