- Durations
- Creating1.6.0+
- Clone2.19.0+
- Humanize1.6.0+
- Milliseconds1.6.0+
- Seconds1.6.0+
- Minutes1.6.0+
- Hours1.6.0+
- Days1.6.0+
- Weeks1.6.0+
- Months1.6.0+
- Years1.6.0+
- Add Time2.1.0+
- Subtract Time2.1.0+
- Using Duration with Diff2.1.0+
- As Unit of Time2.1.0+
- Get Unit of Time2.1.0+
- As JSON2.9.0+
- Is a Duration1.6.0+
- As ISO 8601 String2.8.0+
- Locale2.17.1+
Durations
Moment.js also has duration objects. Where a moment is defined as single points in time, durations are defined as a length of time.
Durations do not have a defined beginning and end date. They are contextless.
A duration is conceptually more similar to '2 hours' than to 'between 2 and 4 pm today'. As such, they are not a good solution to converting between units that depend on context.
For example, a year can be defined as 366 days, 365 days, 365.25 days, 12 months, or 52 weeks. Trying to convert years to days makes no sense without context. It is much better to use moment#diff
for calculating days or years between two moments than to use Durations
.
Creating1.6.0+
moment.duration(Number, String);
moment.duration(Number);
moment.duration(Object);
moment.duration(String);
To create a duration, call moment.duration()
with the length of time in milliseconds.
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.
moment.duration(2, 'seconds');
moment.duration(2, 'minutes');
moment.duration(2, 'hours');
moment.duration(2, 'days');
moment.duration(2, 'weeks');
moment.duration(2, 'months');
moment.duration(2, 'years');
The same shorthand for moment#add
and moment#subtract
works here as well.
Key | Shorthand |
---|---|
years | y |
months | M |
weeks | w |
days | d |
hours | h |
minutes | m |
seconds | s |
milliseconds | ms |
Much like moment#add
, you can pass an object of values if you need multiple different units of measurement.
moment.duration({
seconds: 2,
minutes: 2,
hours: 2,
days: 2,
weeks: 2,
months: 2,
years: 2
});
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
.
moment.duration('23:59:59');
moment.duration('23:59:59.999');
moment.duration('7.23:59:59.999');
moment.duration('23:59'); // added in 2.3.0
As of 2.3.0, moment also supports parsing ISO 8601 durations.
moment.duration('P1Y2M3DT4H5M6S');
moment.duration('P1M');
As of 2.11.0, duration format strings with a space between days and restis supported.
moment.duration('7 23:59:59.999');
As of 2.13.0, mixed negative and positive signs are supported when parsing durations.
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).
moment.duration(NaN);
moment.duration(NaN, 'days');
moment.duration.invalid();
Clone2.19.0+
moment.duration().clone();
Create a clone of a duration. Durations are mutable, just like moment objects,so this lets you get a snapshot, at some point in time.
var d1 = moment.duration();
var d2 = d1.clone();
d1.add(1, 'second');
d1.asMilliseconds() !== d2.asMilliseconds();
Humanize1.6.0+
moment.duration().humanize();
Sometimes, you want all the goodness of moment#from
but you don't want to have to create two moments, you just want to display a length of time.
Enter moment.duration().humanize()
.
moment.duration(1, "minutes").humanize(); // a minute
moment.duration(2, "minutes").humanize(); // 2 minutes
moment.duration(24, "hours").humanize(); // a day
By default, the return string is suffixless. If you want a suffix, pass in true as seen below.
moment.duration(1, "minutes").humanize(true); // in a minute
For suffixes before now, pass in a negative number.
moment.duration(-1, "minutes").humanize(true); // a minute ago
Invalid durations are humanized to the localized version of Invalid Date
.
moment.duration.invalid().humanize(); // Invalid Date
Milliseconds1.6.0+
moment.duration().milliseconds();
moment.duration().asMilliseconds();
To get the number of milliseconds in a duration, use moment.duration().milliseconds()
.
It will return a number between 0 and 999.
moment.duration(500).milliseconds(); // 500
moment.duration(1500).milliseconds(); // 500
moment.duration(15000).milliseconds(); // 0
If you want the length of the duration in milliseconds, use moment.duration().asMilliseconds()
instead.
moment.duration(500).asMilliseconds(); // 500
moment.duration(1500).asMilliseconds(); // 1500
moment.duration(15000).asMilliseconds(); // 15000
Seconds1.6.0+
moment.duration().seconds();
moment.duration().asSeconds();
To get the number of seconds in a duration, use moment.duration().seconds()
.
It will return a number between 0 and 59.
moment.duration(500).seconds(); // 0
moment.duration(1500).seconds(); // 1
moment.duration(15000).seconds(); // 15
If you want the length of the duration in seconds, use moment.duration().asSeconds()
instead.
moment.duration(500).asSeconds(); // 0.5
moment.duration(1500).asSeconds(); // 1.5
moment.duration(15000).asSeconds(); // 15
Minutes1.6.0+
moment.duration().minutes();
moment.duration().asMinutes();
As with the other getters for durations, moment.duration().minutes()
gets the minutes (0 - 59).
moment.duration().asMinutes()
gets the length of the duration in minutes.
Hours1.6.0+
moment.duration().hours();
moment.duration().asHours();
As with the other getters for durations, moment.duration().hours()
gets the hours (0 - 23).
moment.duration().asHours()
gets the length of the duration in hours.
Days1.6.0+
moment.duration().days();
moment.duration().asDays();
As with the other getters for durations, moment.duration().days()
gets the days (0 - 30).
moment.duration().asDays()
gets the length of the duration in days.
Weeks1.6.0+
moment.duration().weeks();
moment.duration().asWeeks();
As with the other getters for durations, moment.duration().weeks()
gets the weeks (0 - 4).
moment.duration().asWeeks()
gets the length of the duration in weeks.
Pay attention that unlike the other getters for duration, weeks are counted as a subset of the days, and are not taken off the days count.
Note: The length of a duration in weeks is defined as 7 days.
Months1.6.0+
moment.duration().months();
moment.duration().asMonths();
As with the other getters for durations, moment.duration().months()
gets the months (0 - 11).
moment.duration().asMonths()
gets the length of the duration in months.
Years1.6.0+
moment.duration().years();
moment.duration().asYears();
As with the other getters for durations, moment.duration().years()
gets the years.
moment.duration().asYears()
gets the length of the duration in years.
Add Time2.1.0+
moment.duration().add(Number, String);
moment.duration().add(Number);
moment.duration().add(Duration);
moment.duration().add(Object);
Mutates the original duration by adding time.
The same keys and shorthands used to create durations can be used here as the second argument.
var a = moment.duration(1, 'd');
var b = moment.duration(2, 'd');
a.add(b).days(); // 3
Note that adding an invalid duration to any other duration results in an invalidduration.
Subtract Time2.1.0+
moment.duration().subtract(Number, String);
moment.duration().subtract(Number);
moment.duration().subtract(Duration);
moment.duration().subtract(Object);
Mutates the original duration by subtracting time.
The same keys and shorthands used to create durations can be used here as the second argument.
var a = moment.duration(3, 'd');
var b = moment.duration(2, 'd');
a.subtract(b).days(); // 1
Note that adding an invalid duration to any other duration results in an invalidduration.
Using Duration with Diff2.1.0+
var duration = moment.duration(x.diff(y))
You can also use duration with moment#diff
to get the duration between two moments. To do so, simply pass the moment#diff
method into moment#duration
as follows:
var x = new moment()
var y = new moment()
var duration = moment.duration(x.diff(y))
// returns duration object with the duration between x and y
See here for more information about moment#diff
.
As Unit of Time2.1.0+
moment.duration().as(String);
As an alternate to Duration#asX
, you can use Duration#as('x')
. All the shorthand keys from moment#add
apply here as well.
duration.as('hours');
duration.as('minutes');
duration.as('seconds');
duration.as('milliseconds');
Invalid durations return NaN
for all units.
Get Unit of Time2.1.0+
moment.duration().get(String);
As an alternate to Duration#x()
getters, you can use Duration#get('x')
. All the shorthand keys from moment#add
apply here as well.
duration.get('hours');
duration.get('minutes');
duration.get('seconds');
duration.get('milliseconds');
Invalid durations return NaN
for all units.
As JSON2.9.0+
moment.duration().toJSON();
When serializing a duration object to JSON, it will be represented as anISO8601 string.
JSON.stringify({
postDuration : moment.duration(5, 'm')
}); // '{"postDuration":"PT5M"}'
Invalid durations return Invalid Date
as json representation.
Is a Duration1.6.0+
moment.isDuration(obj);
To check if a variable is a moment duration object, use moment.isDuration()
.
moment.isDuration() // false
moment.isDuration(new Date()) // false
moment.isDuration(moment()) // false
moment.isDuration(moment.duration()) // true
moment.isDuration(moment.duration(2, 'minutes')) // true
As ISO 8601 String2.8.0+
moment.duration().toISOString();
Returns duration in string as specified by ISO 8601 standard.
moment.duration(1, 'd').toISOString() // "P1D"
Format PnYnMnDTnHnMnS
description:
Unit | Meaning |
---|---|
P | P stands for period. Placed at the start of the duration representation. |
Y | Year |
M | Month |
D | Day |
T | Designator that precedes the time components. |
H | Hour |
M | Minute |
S | Second |
Locale2.17.1+
moment.duration().locale();
moment.duration().locale(String);
You can get or set the locale of a duration using locale(…)
. The locale will affect the duration's string methods, like humanize()
. See the intl section for more information on internationalization generally.
moment.duration(1, "minutes").locale("en").humanize(); // a minute
moment.duration(1, "minutes").locale("fr").humanize(); // une minute
moment.duration(1, "minutes").locale("es").humanize(); // un minuto
Suffixes in humanize()
are also internationalized:
moment.duration(1, "minutes").locale("en").humanize(true); // in a minute
moment.duration(1, "minutes").locale("fr").humanize(true); // dans une minute
moment.duration(1, "minutes").locale("es").humanize(true); // en un minuto
moment.duration(-1, "minutes").locale("en").humanize(true); // a minute ago
moment.duration(-1, "minutes").locale("fr").humanize(true); // il y a une minute
moment.duration(-1, "minutes").locale("es").humanize(true); // hace un minuto