The lastest version of this document is available on Github > datetime-object

Main Contents

TimeSpan Section

dateTime-object also has TimeSPan objects. Where a DateTime is defined as single points in time, durations are defined as a length of time. TimeSpans do not have a defined beginning and end date. They are contextless. A TimeSpan 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.

Constructors

new TimeSpan(number); new TimeSpan(number, unitString); To create a duration, call `new TimeSpan()` with the length of time in milliseconds. new TimeSpan(100);
Key Shorthand
years y
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms

Parsing

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`. TimeSpan.parse('23:59:59'); TimeSpan.parse('23:59:59.999'); TimeSpan.parse('7.23:59:59.999'); TimeSpan.parse('23:59'); This API also supports parsing ISO 8601 durations. TimeSpan.parse('P1Y2M3DT4H5M6S'); TimeSpan.parse('P1M'); duration format strings with a space between days and rest is supported. TimeSpan.parse('7 23:59:59.999'); mixed negative and positive signs are supported when parsing durations. TimeSpan.parse('PT-6H3M')

Clone

Create a clone of a duration. new TimeSpan().clone(); var d1 = new TimeSpan(); var d2 = d1.clone(); d1.add(1, 'second'); d1.asMilliseconds() !== d2.asMilliseconds();

Humanize

new TimeSpan().humanize(); new TimeSpan().humanize(relativeBoolean); new TimeSpan(1, "minutes").humanize(); new TimeSpan(2, "minutes").humanize(); new TimeSpan(24, "hours").humanize();

By default, the return string is suffixless. If you want a suffix, pass in true as seen below.

new TimeSpan(1, "minutes").humanize(true); For suffixes before now, pass in a negative number. new TimeSpan(-1, "minutes").humanize(true);

milliseconds

get the number of milliseconds in a duration new TimeSpan().milliseconds(); return a number between 0 and 999. new TimeSpan(1500).milliseconds(); If you want the length of the duration in milliseconds, use `asMilliseconds()` instead. new TimeSpan(500).asMilliseconds();

Locale

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](#/i18n/) section for more information on internationalization generally. new TimeSpan().locale(); new TimeSpan().locale(String); new TimeSpan(1, "minutes").locale("en").humanize(); new TimeSpan(1, "minutes").locale("fr").humanize(); new TimeSpan(1, "minutes").locale("es").humanize(); Suffixes in `humanize()` are also internationalized: new TimeSpan(1, "minutes").locale("en").humanize(true); new TimeSpan(1, "minutes").locale("fr").humanize(true); new TimeSpan(1, "minutes").locale("es").humanize(true); new TimeSpan(-1, "minutes").locale("en").humanize(true); new TimeSpan(-1, "minutes").locale("fr").humanize(true); new TimeSpan(-1, "minutes").locale("es").humanize(true);

totalSeconds

Get the length of the duration in seconds new TimeSpan(500).totalSeconds();

totalMinutes

Get the length of the duration in minutes new TimeSpan(500).totalMinutes();

totalHours

Get the length of the duration in hours new TimeSpan(500).totalHours();

totalDays

Get the length of the duration in days new TimeSpan(5500).totalDays();

totalYears

Get the length of the duration in years new TimeSpan(550000).totalYears();

addMiliseconds

Return a new TimeSpan adding miliseconds to a TimeSpan
new TimeSpan().addMiliseconds(Number);

toJSON

new TimeSpan().toJSON(); When serializing a duration object to JSON, it will be represented as an ISO8601 string. JSON.stringify({ postDuration : new TimeSpan(5, 'm') });

As ISO 8601 String

new TimeSpan().toISOString(); Returns duration in string as specified by ISO 8601 standard . new TimeSpan(1, 'd').toISOString()

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