ISO 8601 duration support for Moment.js


ISO 8601 duration support for Moment.js.

var oneWeek = moment.isoDuration('P1W');
var twoDaysThreeHours = moment.isoDuration('P2DT3H');
var fourSeconds = moment.isoDuration('PT4S');
var allMixed = moment.isoDuration('P1Y2M3DT4H5M6S');

The ISO spec for durations doesn't define a millisecond unit, but I consider it useful so added it anyway under the Z unit.

var twentyMilliseconds = moment.isoDuration('PT20Z');

There's also a helper method to get a duration value in ISO format.

var delay = moment.duration(100);
var isoString = delay.toISOString(duration); // PT100Z 


moment-duration is freely distributable under the terms of the MIT license.