The TimeSpan
class is a utility for managing and manipulating time intervals in TypeScript/JavaScript. It provides a simple, object-oriented way to perform arithmetic and comparison operations on time spans. This class can be used to represent durations in milliseconds, seconds, minutes, hours, or days.
- Creation Methods: Create
TimeSpan
objects from various inputs (current time,Date
objects, milliseconds, seconds, minutes, hours, or days). - Conversion Methods: Convert a
TimeSpan
to milliseconds, seconds, minutes, hours, or days. - Arithmetic Operations: Add, subtract, multiply, and divide
TimeSpan
objects. - Comparison Methods: Compare
TimeSpan
objects using equality, less than, greater than, etc. - Time Checks: Check if the
TimeSpan
represents a past or future time.
To use the TimeSpan
utility, import it into your TypeScript or JavaScript project.
import { TimeSpan } from './TimeSpan';
const spanFromNow = TimeSpan.fromNow(); // From the current time
const spanFromDate = TimeSpan.fromDate(new Date()); // From a Date object
const spanFromMilliseconds = TimeSpan.fromMilliseconds(5000); // From milliseconds
const spanFromSeconds = TimeSpan.fromSeconds(5); // From seconds
const spanFromMinutes = TimeSpan.fromMinutes(2); // From minutes
const spanFromHours = TimeSpan.fromHours(1); // From hours
const spanFromDays = TimeSpan.fromDays(1); // From days
const span = TimeSpan.fromMilliseconds(90061);
console.log(span.milliseconds()); // 90061
console.log(span.seconds()); // 90 (rounded)
console.log(span.minutes()); // 1 (rounded)
console.log(span.hours()); // 0 (rounded)
console.log(span.days()); // 0 (rounded)
const span1 = TimeSpan.fromSeconds(30);
const span2 = TimeSpan.fromSeconds(45);
const sum = span1.add(span2); // Adds two TimeSpans
const difference = span2.subtract(span1); // Subtracts two TimeSpans
const product = span1.multiply(2); // Multiplies a TimeSpan by a factor
const quotient = span2.divide(2); // Divides a TimeSpan by a divisor
const span1 = TimeSpan.fromSeconds(30);
const span2 = TimeSpan.fromSeconds(45);
console.log(span1.equals(span2)); // false
console.log(span1.lessThan(span2)); // true
console.log(span1.lessThanOrEqual(span2)); // true
console.log(span2.greaterThan(span1)); // true
console.log(span2.greaterThanOrEqual(span1)); // true
const futureTimeSpan = TimeSpan.fromMilliseconds(Date.now() + 10000);
const pastTimeSpan = TimeSpan.fromMilliseconds(Date.now() - 10000);
console.log(futureTimeSpan.isFuture()); // true
console.log(pastTimeSpan.isPast()); // true
const span = TimeSpan.fromMilliseconds(123456);
console.log(span.toString()); // '123456'
TimeSpan.fromNow(): TimeSpan
— Creates aTimeSpan
from the current time.TimeSpan.fromDate(date: Date): TimeSpan
— Creates aTimeSpan
from aDate
object.TimeSpan.fromMilliseconds(milliseconds: number): TimeSpan
— Creates aTimeSpan
from milliseconds.TimeSpan.fromSeconds(seconds: number): TimeSpan
— Creates aTimeSpan
from seconds.TimeSpan.fromMinutes(minutes: number): TimeSpan
— Creates aTimeSpan
from minutes.TimeSpan.fromHours(hours: number): TimeSpan
— Creates aTimeSpan
from hours.TimeSpan.fromDays(days: number): TimeSpan
— Creates aTimeSpan
from days.
milliseconds(): number
— Returns the total number of milliseconds.seconds(round = true): number
— Returns the total number of seconds, optionally rounded.minutes(round = true): number
— Returns the total number of minutes, optionally rounded.hours(round = true): number
— Returns the total number of hours, optionally rounded.days(round = true): number
— Returns the total number of days, optionally rounded.add(other: TimeSpan): TimeSpan
— Adds anotherTimeSpan
to this one.subtract(other: TimeSpan): TimeSpan
— Subtracts anotherTimeSpan
from this one.multiply(factor: number): TimeSpan
— Multiplies theTimeSpan
by a factor.divide(divisor: number): TimeSpan
— Divides theTimeSpan
by a divisor.equals(other: TimeSpan): boolean
— Checks if theTimeSpan
is equal to another.lessThan(other: TimeSpan): boolean
— Checks if theTimeSpan
is less than another.lessThanOrEqual(other: TimeSpan): boolean
— Checks if theTimeSpan
is less than or equal to another.greaterThan(other: TimeSpan): boolean
— Checks if theTimeSpan
is greater than another.greaterThanOrEqual(other: TimeSpan): boolean
— Checks if theTimeSpan
is greater than or equal to another.isPast(): boolean
— Checks if theTimeSpan
represents a time in the past.isFuture(): boolean
— Checks if theTimeSpan
represents a time in the future.toString(): string
— Converts theTimeSpan
to a string of milliseconds.
The TIMES_IN_MILLISECONDS
constant is used to define time unit conversions.
export const TIMES_IN_MILLISECONDS = {
SECOND: 1_000,
MINUTE: 1_000 * 60,
HOUR: 1_000 * 60 * 60,
DAY: 1_000 * 60 * 60 * 24,
WEEK: 1_000 * 60 * 60 * 24 * 7,
MONTH: 1_000 * 60 * 60 * 24 * 30,
YEAR: 1_000 * 60 * 60 * 24 * 365,
} as const;