Difference1.0.0+

  1. moment().diff(Moment|String|Number|Date|Array);
  2. moment().diff(Moment|String|Number|Date|Array, String);
  3. moment().diff(Moment|String|Number|Date|Array, String, Boolean);

To get the difference in milliseconds, use moment#diff like you would use moment#from.

  1. var a = moment([2007, 0, 29]);
  2. var b = moment([2007, 0, 28]);
  3. a.diff(b) // 86400000

To get the difference in another unit of measurement, pass that measurement as the second argument.

  1. var a = moment([2007, 0, 29]);
  2. var b = moment([2007, 0, 28]);
  3. a.diff(b, 'days') // 1

To get the duration of a difference between two moments, you can pass diff as an argument into moment#duration. See the docs on moment#duration for more info.

The supported measurements are years, months, weeks, days, hours, minutes, and seconds. For ease of development, the singular forms are supported as of 2.0.0. Units of measurement other than milliseconds are available in version 1.1.1.

By default, moment#diff will truncate the result to zero decimal places, returning an integer. If you want a floating point number, pass true as the third argument. Before 2.0.0, moment#diff returned a number rounded to the nearest integer, not a truncated number.

  1. var a = moment([2008, 9]);
  2. var b = moment([2007, 0]);
  3. a.diff(b, 'years'); // 1
  4. a.diff(b, 'years', true); // 1.75

If the moment is earlier than the moment you are passing to moment.fn.diff, the return value will be negative.

  1. var a = moment();
  2. var b = moment().add(1, 'seconds');
  3. a.diff(b) // -1000
  4. b.diff(a) // 1000

An easy way to think of this is by replacing .diff( with a minus operator.

  1. // a < b
  2. a.diff(b) // a - b < 0
  3. b.diff(a) // b - a > 0

Month and year diffs

moment#diff has some special handling for month and year diffs. It is optimized to ensure that two months with the same date are always a whole number apart.

So Jan 15 to Feb 15 should be exactly 1 month.

Feb 28 to Mar 28 should be exactly 1 month.

Feb 28 2011 to Feb 28 2012 should be exactly 1 year.

See more discussion on the month and year diffs here

This change to month and year diffs was made in 2.0.0.As of version 2.9.0 diff also support quarter unit.