Skip to main content

Class: DateLib

A wrapper class around date-fns sharing the same options.

Since

9.2.0

Example

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

Constructors

new DateLib()

new DateLib(options?, overrides?): DateLib

Creates an instance of DateLib.

Parameters

ParameterTypeDescription
options?DateLibOptionsThe options for the date library.
overrides?Partial<DateLib>Overrides for the date library functions.

Returns

DateLib

Defined in

src/classes/DateLib.ts:116

Methods

addDays()

addDays(date, amount): Date

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.

Defined in

src/classes/DateLib.ts:216


addMonths()

addMonths(date, amount): Date

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.

Defined in

src/classes/DateLib.ts:227


addWeeks()

addWeeks(date, amount): Date

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.

Defined in

src/classes/DateLib.ts:238


addYears()

addYears(date, amount): Date

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.

Defined in

src/classes/DateLib.ts:249


differenceInCalendarDays()

differenceInCalendarDays(dateLeft, dateRight): number

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.

Defined in

src/classes/DateLib.ts:260


differenceInCalendarMonths()

differenceInCalendarMonths(dateLeft, dateRight): number

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.

Defined in

src/classes/DateLib.ts:274


eachMonthOfInterval()

eachMonthOfInterval(interval): Date[]

Returns the months between the given dates.

Parameters

ParameterTypeDescription
intervalInterval<Date, DateArg<Date>>The interval to get the months for.

Returns

Date[]

Defined in

src/classes/DateLib.ts:286


endOfBroadcastWeek()

endOfBroadcastWeek(date, dateLib?): Date

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

Parameters

ParameterTypeDescription
dateDateThe original date.
dateLib?DateLib-

Returns

Date

The end of the broadcast week.

Defined in

src/classes/DateLib.ts:299


endOfISOWeek()

endOfISOWeek(date): Date

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

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The end of the ISO week.

Defined in

src/classes/DateLib.ts:312


endOfMonth()

endOfMonth(date): Date

Returns the end of the month for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The end of the month.

Defined in

src/classes/DateLib.ts:322


endOfWeek()

endOfWeek(date, options?): Date

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.

Defined in

src/classes/DateLib.ts:332


endOfYear()

endOfYear(date): Date

Returns the end of the year for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The end of the year.

Defined in

src/classes/DateLib.ts:345


format()

format(date, formatStr, options?): string

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.

Defined in

src/classes/DateLib.ts:356


formatNumber()

formatNumber(value): string

Format number using the custom numbering system.

Parameters

ParameterTypeDescription
valuenumberThe number to format.

Returns

string

The formatted number.

Since

9.5.0

Defined in

src/classes/DateLib.ts:163


getISOWeek()

getISOWeek(date): number

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.

Defined in

src/classes/DateLib.ts:377


getMonth()

getMonth(date): number

Returns the month of the given date.

Parameters

ParameterTypeDescription
dateDateThe date to get the month for.

Returns

number

The month.

Defined in

src/classes/DateLib.ts:387


getWeek()

getWeek(date, options?): number

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.

Defined in

src/classes/DateLib.ts:407


getYear()

getYear(date): number

Returns the year of the given date.

Parameters

ParameterTypeDescription
dateDateThe date to get the year for.

Returns

number

The year.

Defined in

src/classes/DateLib.ts:397


isAfter()

isAfter(date, dateToCompare): boolean

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.

Defined in

src/classes/DateLib.ts:421


isBefore()

isBefore(date, dateToCompare): boolean

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.

Defined in

src/classes/DateLib.ts:435


isSameDay()

isSameDay(dateLeft, dateRight): boolean

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.

Defined in

src/classes/DateLib.ts:459


isSameMonth()

isSameMonth(dateLeft, dateRight): boolean

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.

Defined in

src/classes/DateLib.ts:473


isSameYear()

isSameYear(dateLeft, dateRight): boolean

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.

Defined in

src/classes/DateLib.ts:487


max()

max(dates): Date

Returns the latest date in the given array of dates.

Parameters

ParameterTypeDescription
datesDate[]The array of dates to compare.

Returns

Date

The latest date.

Defined in

src/classes/DateLib.ts:500


min()

min(dates): Date

Returns the earliest date in the given array of dates.

Parameters

ParameterTypeDescription
datesDate[]The array of dates to compare.

Returns

Date

The earliest date.

Defined in

src/classes/DateLib.ts:510


newDate()

newDate(year, monthIndex, date): Date

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

Parameters

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

Returns

Date

The new date object.

Since

9.5.0

Defined in

src/classes/DateLib.ts:199


setMonth()

setMonth(date, month): Date

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.

Defined in

src/classes/DateLib.ts:521


setYear()

setYear(date, year): Date

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.

Defined in

src/classes/DateLib.ts:532


startOfBroadcastWeek()

startOfBroadcastWeek(date, dateLib?): Date

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

Parameters

ParameterTypeDescription
dateDateThe original date.
dateLib?DateLib-

Returns

Date

The start of the broadcast week.

Defined in

src/classes/DateLib.ts:542


startOfDay()

startOfDay(date): Date

Returns the start of the day for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the day.

Defined in

src/classes/DateLib.ts:555


startOfISOWeek()

startOfISOWeek(date): Date

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

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the ISO week.

Defined in

src/classes/DateLib.ts:565


startOfMonth()

startOfMonth(date): Date

Returns the start of the month for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the month.

Defined in

src/classes/DateLib.ts:575


startOfWeek()

startOfWeek(date): Date

Returns the start of the week for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the week.

Defined in

src/classes/DateLib.ts:585


startOfYear()

startOfYear(date): Date

Returns the start of the year for the given date.

Parameters

ParameterTypeDescription
dateDateThe original date.

Returns

Date

The start of the year.

Defined in

src/classes/DateLib.ts:597


today()

today(): Date

Creates a new date object to the today's date.

Returns

Date

The new date object.

Since

9.5.0

Defined in

src/classes/DateLib.ts:180

Properties

Date

Date: DateConstructor

Reference to the built-in Date constructor.

Deprecated

Use newDate() or today().

Defined in

src/classes/DateLib.ts:172


isDate()

isDate: (value) => value is Date

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.

Defined in

src/classes/DateLib.ts:448


options

readonly options: DateLibOptions

The options for the date library.

Defined in

src/classes/DateLib.ts:105


overrides?

readonly optional overrides: Partial<DateLib>

Overrides for the date library functions.

Defined in

src/classes/DateLib.ts:108