Date Extensions

v0.1.0

The Date Extension methods extend the global Date Object´s Prototype.

Usage

Import the extensions part as early as possible to make sure the global Object Prototypes are extended.
Your IDE might offer the typing without importing this init function as they are declared globally.
Date Extensions are using dayjs internally.

import { initDateExtensions } from '@oardi/ts-utils';
initDateExtensions();

Initialization is idempotent and keeps the installed methods unchanged. If Date.prototype already has a foreign own property with one of the extension names, initialization throws before installing any Date extensions instead of overwriting the existing property.

add

const date = new Date();
const later = date.add(15, 'minutes');

Date extension methods return new Date values and do not mutate the original date.

diff

const startDate = new Date();
const endDate = new Date();
endDate.diff(startDate, 'minutes');

startOf and endOf

const date = new Date();
const startOfDay = date.startOf('day');
const endOfDay = date.endOf('day');

firstDayOfMonth and lastDayOfMonth

const date = new Date();
const firstDay = date.firstDayOfMonth();
const lastDay = date.lastDayOfMonth();

format

const date = new Date('2026-08-15T12:00:00');
date.format('YYYY-MM-DD');
// '2026-08-15'

isAfter

const startDate = new Date();
const endDate = new Date();
startDate.isAfter(endDate);

isBefore

const startDate = new Date();
const endDate = new Date();
startDate.isBefore(endDate);

isValid

new Date().isValid();
// true

isoWeek

const date = new Date();
date.isoWeek();
// ISO week number

set

const date = new Date();
const withNewHour = date.set('hour', 9);

subtract

const date = new Date();
const previousWeek = date.subtract(1, 'week');

Previous:Extensions

← Array Extension

Next:Extensions

String Extension →