Skip to main content

TimeSpan

The TimeSpan class is used for representing time interval.

info

Note TimeSpan cannot be negative.

Creating a TimeSpan

Creating TimeSpan from milliseconds:

const timeSpan = TimeSpan.fromMilliseconds(100);

Creating TimeSpan from seconds:

const timeSpan = TimeSpan.fromSeconds(30);

Creating TimeSpan from minutes:

const timeSpan = TimeSpan.fromMinutes(15);

Creating TimeSpan from hours:

const timeSpan = TimeSpan.fromHours(1);

Creating TimeSpan from days:

const timeSpan = TimeSpan.fromDays(1);

Creating TimeSpan from date range:

const timeSpan = TimeSpan.fromDateRange(
new Date("2000-01-01"),
new Date("2010-01-01"),
);

Adding time to TimeSpan

You can add milliseconds to a TimeSpan:

timeSpan.addMilliseconds(200);

You can add seconds to a TimeSpan:`

timeSpan.addSeconds(30);

You can add minutes to a TimeSpan:

timeSpan.addMinutes(20);

You can add hours to a TimeSpan:

timeSpan.addHours(2);

You can add days to a TimeSpan:

timeSpan.addDays(14);

You can add 2 TimeSpan together:

timeSpan.addTimeSpan(TimeSpan.fromDays(14).addHours(20));

Subtracting time from TimeSpan

You can subtract milliseconds from a TimeSpan:

timeSpan.subtractMilliseconds(200);

You can subtract seconds from a TimeSpan:`

timeSpan.subtractSeconds(30);

You can subtract minutes from a TimeSpan:

timeSpan.subtractMinutes(20);

You can subtract hours from a TimeSpan:

timeSpan.subtractHours(2);

You can subtract days from a TimeSpan:

timeSpan.subtractDays(14);

You can subtract 2 TimeSpan together:

timeSpan.subtractTimeSpan(TimeSpan.fromDays(14).addHours(20));

Multiplying and dividing a TimeSpan

Dividing a timespan:

// Will be now 100 miliseconds
TimeSpan.fromMilliseconds(200).divide(2);

Multiplying a timespan:

// Will be now 400 miliseconds
TimeSpan.fromMilliseconds(200).multiply(2);

Converting a TimeSpan

You can get amount of milliseconds contained in the TimeSpan:

TimeSpan.fromSeconds(1).toMilliseconds();

You can get amount of seconds contained in the TimeSpan:

TimeSpan.fromMinutes(1).toSeconds();

You can get amount of minutes contained in the TimeSpan:

TimeSpan.fromHour(1).toMinutes();

You can get amount of hours contained in the TimeSpan:

TimeSpan.fromDays(1).toHours();

You can get amount of days contained in the TimeSpan:

TimeSpan.fromHour(48).toDays();

You can get end date relative to a start date:

// Will return date of "2002-01-01"
TimeSpan.fromDays(365).toEndDate(new Date("2001-01-01"));

You can get start date relative to a end date:

// Will return date of "2000-01-01"
TimeSpan.fromDays(365).toStartDate(new Date("2001-01-01"));

Serialization and deserialization of TimeSpan

The TimeSpan class supports serialization and deserialization, allowing you to easily convert instances to and from serialized formats. However, registration is required first:

import { Serde } from "@daiso-tech/core/serde";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters";
import { TimeSpan } from "@daiso-tech/core/utilities";

const serde = new Serde(new SuperJsonSerdeAdapter());

serde.registerClass(TimeSpan);

const timeSpan = TimeSpan.fromSeconds(12);
const serializedTimeSpan = serde.serialize(timeSpan);
const deserializedTimeSpan = serde.deserialize(serializedTimeSpan);

// logs false
console.log(serializedTimeSpan === deserializedTimeSpan);