Creating1.6.0+

  1. moment.duration(Number, String);
  2. moment.duration(Number);
  3. moment.duration(Object);
  4. moment.duration(String);

To create a duration, call moment.duration() with the length of time in milliseconds.

  1. moment.duration(100); // 100 milliseconds

If you want to create a moment with a unit of measurement other than milliseconds, you can pass the unit of measurement as well.

  1. moment.duration(2, 'seconds');
  2. moment.duration(2, 'minutes');
  3. moment.duration(2, 'hours');
  4. moment.duration(2, 'days');
  5. moment.duration(2, 'weeks');
  6. moment.duration(2, 'months');
  7. moment.duration(2, 'years');

The same shorthand for moment#add and moment#subtract works here as well.

KeyShorthand
yearsy
monthsM
weeksw
daysd
hoursh
minutesm
secondss
millisecondsms

Much like moment#add, you can pass an object of values if you need multiple different units of measurement.

  1. moment.duration({
  2. seconds: 2,
  3. minutes: 2,
  4. hours: 2,
  5. days: 2,
  6. weeks: 2,
  7. months: 2,
  8. years: 2
  9. });

As of 2.1.0, moment supports parsing ASP.NET style time spans. The following formats are supported.

The format is an hour, minute, second string separated by colons like 23:59:59. The number of days can be prefixed with a dot separator like so 7.23:59:59. Partial seconds are supported as well 23:59:59.999.

  1. moment.duration('23:59:59');
  2. moment.duration('23:59:59.999');
  3. moment.duration('7.23:59:59.999');
  4. moment.duration('23:59'); // added in 2.3.0

As of 2.3.0, moment also supports parsing ISO 8601 durations.

  1. moment.duration('P1Y2M3DT4H5M6S');
  2. moment.duration('P1M');

As of 2.11.0, duration format strings with a space between days and restis supported.

  1. moment.duration('7 23:59:59.999');

As of 2.13.0, mixed negative and positive signs are supported when parsing durations.

  1. moment.duration('PT-6H3M')

As of 2.18.0, invalid durations are supported, similarly to invalidmoments. To create an invalid duration you can pass NaN for a value ofa unit.

In upcoming releases expect invalid durations to cover more cases (likenull values for units).

  1. moment.duration(NaN);
  2. moment.duration(NaN, 'days');
  3. moment.duration.invalid();