Skip to main content

Class: DateLib

Defined in: src/classes/DateLib.ts:105

A wrapper class around date-fns that provides utility methods for date manipulation and formatting.

Since

9.2.0

Example

const dateLib = new DateLib({ locale: es });
const newDate = dateLib.addDays(new Date(), 5);

Constructors

Constructor

new DateLib(options?, overrides?): DateLib

Defined in: src/classes/DateLib.ts:118

Creates an instance of DateLib.

Parameters

ParameterTypeDescription
options?DateLibOptionsConfiguration options for the date library.
overrides?Partial<DateLib>Custom overrides for the date library functions.

Returns

DateLib

Methods

addDays()

addDays(date, amount): Date

Defined in: src/classes/DateLib.ts:222

Adds the specified number of days to the given date.

Parameters

ParameterTypeDescription
dateDateThe date to add days to.
amountnumberThe number of days to add.

Returns

Date

The new date with the days added.


addMonths()

addMonths(date, amount): Date

Defined in: src/classes/DateLib.ts:235

Adds the specified number of months to the given date.

Parameters

ParameterTypeDescription
dateDateThe date to add months to.
amountnumberThe number of months to add.

Returns

Date

The new date with the months added.


addWeeks()

addWeeks(date, amount): Date

Defined in: src/classes/DateLib.ts:248

Adds the specified number of weeks to the given date.

Parameters

ParameterTypeDescription
dateDateThe date to add weeks to.
amountnumberThe number of weeks to add.

Returns

Date

The new date with the weeks added.


addYears()

addYears(date, amount): Date

Defined in: src/classes/DateLib.ts:261

Adds the specified number of years to the given date.

Parameters

ParameterTypeDescription
dateDateThe date to add years to.
amountnumberThe number of years to add.

Returns

Date

The new date with the years added.


differenceInCalendarDays()

differenceInCalendarDays(dateLeft, dateRight): number

Defined in: src/classes/DateLib.ts:274

Returns the number of calendar days between the given dates.

Parameters

ParameterTypeDescription
dateLeftDateThe later date.
dateRightDateThe earlier date.

Returns

number

The number of calendar days between the dates.


differenceInCalendarMonths()

differenceInCalendarMonths(dateLeft, dateRight): number

Defined in: src/classes/DateLib.ts:287

Returns the number of calendar months between the given dates.

Parameters

ParameterTypeDescription
dateLeftDateThe later date.
dateRightDateThe earlier date.

Returns

number

The number of calendar months between the dates.


eachMonthOfInterval()

eachMonthOfInterval(interval): Date[]

Defined in: src/classes/DateLib.ts:298

Returns the months between the given dates.

Parameters

ParameterTypeDescription
intervalIntervalThe interval to get the months for.

Returns

Date[]


endOfBroadcastWeek()

endOfBroadcastWeek(date): Date

Defined in: src/classes/DateLib.ts:310

Returns the end of the broadcast week for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The end of the broadcast week.


endOfISOWeek()

endOfISOWeek(date): Date

Defined in: src/classes/DateLib.ts:322

Returns the end of the ISO week for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The end of the ISO week.


endOfMonth()

endOfMonth(date): Date

Defined in: src/classes/DateLib.ts:334

Returns the end of the month for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The end of the month.


endOfWeek()

endOfWeek(date, options?): Date

Defined in: src/classes/DateLib.ts:346

Returns the end of the week for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.
options?EndOfWeekOptions<Date>-

Returns

Date

The end of the week.


endOfYear()

endOfYear(date): Date

Defined in: src/classes/DateLib.ts:358

Returns the end of the year for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The end of the year.


format()

format(date, formatStr, options?): string

Defined in: src/classes/DateLib.ts:371

Formats the given date using the specified format string.

Parameters

ParameterTypeDescription
dateDateThe date to format.
formatStrstringThe format string.
options?FormatOptions-

Returns

string

The formatted date string.


formatNumber()

formatNumber(value): string

Defined in: src/classes/DateLib.ts:169

Formats a number using the configured numbering system.

Parameters

ParameterTypeDescription
valuestring | numberThe number to format.

Returns

string

The formatted number as a string.

Since

9.5.0


getISOWeek()

getISOWeek(date): number

Defined in: src/classes/DateLib.ts:391

Returns the ISO week number for the given date.

Parameters

ParameterTypeDescription
dateDateThe date to get the ISO week number for.

Returns

number

The ISO week number.


getMonth()

getMonth(date, options?): number

Defined in: src/classes/DateLib.ts:403

Returns the month of the given date.

Parameters

ParameterTypeDescription
dateDateThe date to get the month for.
options?GetMonthOptions-

Returns

number

The month.


getWeek()

getWeek(date, options?): number

Defined in: src/classes/DateLib.ts:427

Returns the local week number for the given date.

Parameters

ParameterTypeDescription
dateDateThe date to get the week number for.
options?GetWeekOptions-

Returns

number

The week number.


getYear()

getYear(date, options?): number

Defined in: src/classes/DateLib.ts:415

Returns the year of the given date.

Parameters

ParameterTypeDescription
dateDateThe date to get the year for.
options?GetYearOptions-

Returns

number

The year.


isAfter()

isAfter(date, dateToCompare): boolean

Defined in: src/classes/DateLib.ts:440

Checks if the first date is after the second date.

Parameters

ParameterTypeDescription
dateDateThe date to compare.
dateToCompareDateThe date to compare with.

Returns

boolean

True if the first date is after the second date.


isBefore()

isBefore(date, dateToCompare): boolean

Defined in: src/classes/DateLib.ts:453

Checks if the first date is before the second date.

Parameters

ParameterTypeDescription
dateDateThe date to compare.
dateToCompareDateThe date to compare with.

Returns

boolean

True if the first date is before the second date.


isSameDay()

isSameDay(dateLeft, dateRight): boolean

Defined in: src/classes/DateLib.ts:478

Checks if the given dates are on the same day.

Parameters

ParameterTypeDescription
dateLeftDateThe first date to compare.
dateRightDateThe second date to compare.

Returns

boolean

True if the dates are on the same day.


isSameMonth()

isSameMonth(dateLeft, dateRight): boolean

Defined in: src/classes/DateLib.ts:491

Checks if the given dates are in the same month.

Parameters

ParameterTypeDescription
dateLeftDateThe first date to compare.
dateRightDateThe second date to compare.

Returns

boolean

True if the dates are in the same month.


isSameYear()

isSameYear(dateLeft, dateRight): boolean

Defined in: src/classes/DateLib.ts:504

Checks if the given dates are in the same year.

Parameters

ParameterTypeDescription
dateLeftDateThe first date to compare.
dateRightDateThe second date to compare.

Returns

boolean

True if the dates are in the same year.


max()

max(dates): Date

Defined in: src/classes/DateLib.ts:516

Returns the latest date in the given array of dates.

Parameters

ParameterTypeDescription
datesDate[]The array of dates to compare.

Returns

Date

The latest date.


min()

min(dates): Date

Defined in: src/classes/DateLib.ts:526

Returns the earliest date in the given array of dates.

Parameters

ParameterTypeDescription
datesDate[]The array of dates to compare.

Returns

Date

The earliest date.


newDate()

newDate(year, monthIndex, date): Date

Defined in: src/classes/DateLib.ts:205

Creates a new Date object with the specified year, month, and day.

Parameters

ParameterTypeDescription
yearnumberThe year.
monthIndexnumberThe month (0-11).
datenumberThe day of the month.

Returns

Date

A new Date object.

Since

9.5.0


setMonth()

setMonth(date, month): Date

Defined in: src/classes/DateLib.ts:537

Sets the month of the given date.

Parameters

ParameterTypeDescription
dateDateThe date to set the month on.
monthnumberThe month to set (0-11).

Returns

Date

The new date with the month set.


setYear()

setYear(date, year): Date

Defined in: src/classes/DateLib.ts:550

Sets the year of the given date.

Parameters

ParameterTypeDescription
dateDateThe date to set the year on.
yearnumberThe year to set.

Returns

Date

The new date with the year set.


startOfBroadcastWeek()

startOfBroadcastWeek(date, dateLib): Date

Defined in: src/classes/DateLib.ts:562

Returns the start of the broadcast week for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.
dateLibDateLib-

Returns

Date

The start of the broadcast week.


startOfDay()

startOfDay(date): Date

Defined in: src/classes/DateLib.ts:574

Returns the start of the day for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the day.


startOfISOWeek()

startOfISOWeek(date): Date

Defined in: src/classes/DateLib.ts:586

Returns the start of the ISO week for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the ISO week.


startOfMonth()

startOfMonth(date): Date

Defined in: src/classes/DateLib.ts:598

Returns the start of the month for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the month.


startOfWeek()

startOfWeek(date, options?): Date

Defined in: src/classes/DateLib.ts:610

Returns the start of the week for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.
options?StartOfWeekOptions<Date>-

Returns

Date

The start of the week.


startOfYear()

startOfYear(date): Date

Defined in: src/classes/DateLib.ts:622

Returns the start of the year for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the year.


today()

today(): Date

Defined in: src/classes/DateLib.ts:186

Creates a new Date object representing today's date.

Returns

Date

A Date object for today's date.

Since

9.5.0

Properties

Date

Date: DateConstructor

Defined in: src/classes/DateLib.ts:178

Reference to the built-in Date constructor.

Deprecated

Use newDate() or today().


isDate()

isDate: (value) => value is Date

Defined in: src/classes/DateLib.ts:465

Checks if the given value is a Date object.

Parameters

ParameterTypeDescription
valueunknownThe value to check.

Returns

value is Date

True if the value is a Date object.


options

readonly options: DateLibOptions

Defined in: src/classes/DateLib.ts:107

The options for configuring the date library.


overrides?

readonly optional overrides: Partial<DateLib>

Defined in: src/classes/DateLib.ts:110

Overrides for the default date library functions.