Class: DateLib
Defined in: src/classes/DateLib.ts:103
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
Defined in: src/classes/DateLib.ts:116
Creates an instance of DateLib.
Parameters
Parameter | Type | Description |
---|---|---|
options ? | DateLibOptions | The options for the date library. |
overrides ? | Partial <DateLib > | Overrides for the date library functions. |
Returns
Methods
addDays()
addDays(
date
,amount
):Date
Defined in: src/classes/DateLib.ts:216
Adds the specified number of days to the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to add days to. |
amount | number | The 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:227
Adds the specified number of months to the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to add months to. |
amount | number | The 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:238
Adds the specified number of weeks to the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to add weeks to. |
amount | number | The 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:249
Adds the specified number of years to the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to add years to. |
amount | number | The 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:260
Returns the number of calendar days between the given dates.
Parameters
Parameter | Type | Description |
---|---|---|
dateLeft | Date | The later date. |
dateRight | Date | The earlier date. |
Returns
number
The number of calendar days between the dates.
differenceInCalendarMonths()
differenceInCalendarMonths(
dateLeft
,dateRight
):number
Defined in: src/classes/DateLib.ts:274
Returns the number of calendar months between the given dates.
Parameters
Parameter | Type | Description |
---|---|---|
dateLeft | Date | The later date. |
dateRight | Date | The earlier date. |
Returns
number
The number of calendar months between the dates.
eachMonthOfInterval()
eachMonthOfInterval(
interval
):Date
[]
Defined in: src/classes/DateLib.ts:286
Returns the months between the given dates.
Parameters
Parameter | Type | Description |
---|---|---|
interval | Interval <Date > | The interval to get the months for. |
Returns
Date
[]
endOfBroadcastWeek()
endOfBroadcastWeek(
date
,dateLib
?):Date
Defined in: src/classes/DateLib.ts:299
Returns the end of the broadcast week for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
dateLib ? | DateLib | - |
Returns
Date
The end of the broadcast week.
endOfISOWeek()
endOfISOWeek(
date
):Date
Defined in: src/classes/DateLib.ts:312
Returns the end of the ISO week for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
Returns
Date
The end of the ISO week.
endOfMonth()
endOfMonth(
date
):Date
Defined in: src/classes/DateLib.ts:322
Returns the end of the month for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
Returns
Date
The end of the month.
endOfWeek()
endOfWeek(
date
,options
?):Date
Defined in: src/classes/DateLib.ts:332
Returns the end of the week for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
options ? | EndOfWeekOptions | - |
Returns
Date
The end of the week.
endOfYear()
endOfYear(
date
):Date
Defined in: src/classes/DateLib.ts:345
Returns the end of the year for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
Returns
Date
The end of the year.
format()
format(
date
,formatStr
,options
?):string
Defined in: src/classes/DateLib.ts:356
Formats the given date using the specified format string.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to format. |
formatStr | string | The format string. |
options ? | FormatOptions | - |
Returns
string
The formatted date string.
formatNumber()
formatNumber(
value
):string
Defined in: src/classes/DateLib.ts:163
Format number using the custom numbering system.
Parameters
Parameter | Type | Description |
---|---|---|
value | number | The number to format. |
Returns
string
The formatted number.
Since
9.5.0
getISOWeek()
getISOWeek(
date
):number
Defined in: src/classes/DateLib.ts:377
Returns the ISO week number for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to get the ISO week number for. |
Returns
number
The ISO week number.
getMonth()
getMonth(
date
):number
Defined in: src/classes/DateLib.ts:387
Returns the month of the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to get the month for. |
Returns
number
The month.
getWeek()
getWeek(
date
,options
?):number
Defined in: src/classes/DateLib.ts:407
Returns the local week number for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to get the week number for. |
options ? | GetWeekOptions | - |
Returns
number
The week number.
getYear()
getYear(
date
):number
Defined in: src/classes/DateLib.ts:397
Returns the year of the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to get the year for. |
Returns
number
The year.
isAfter()
isAfter(
date
,dateToCompare
):boolean
Defined in: src/classes/DateLib.ts:421
Checks if the first date is after the second date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to compare. |
dateToCompare | Date | The 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:435
Checks if the first date is before the second date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to compare. |
dateToCompare | Date | The 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:459
Checks if the given dates are on the same day.
Parameters
Parameter | Type | Description |
---|---|---|
dateLeft | Date | The first date to compare. |
dateRight | Date | The 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:473
Checks if the given dates are in the same month.
Parameters
Parameter | Type | Description |
---|---|---|
dateLeft | Date | The first date to compare. |
dateRight | Date | The 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:487
Checks if the given dates are in the same year.
Parameters
Parameter | Type | Description |
---|---|---|
dateLeft | Date | The first date to compare. |
dateRight | Date | The 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:500
Returns the latest date in the given array of dates.
Parameters
Parameter | Type | Description |
---|---|---|
dates | Date [] | The array of dates to compare. |
Returns
Date
The latest date.
min()
min(
dates
):Date
Defined in: src/classes/DateLib.ts:510
Returns the earliest date in the given array of dates.
Parameters
Parameter | Type | Description |
---|---|---|
dates | Date [] | The array of dates to compare. |
Returns
Date
The earliest date.
newDate()
newDate(
year
,monthIndex
,date
):Date
Defined in: src/classes/DateLib.ts:199
Creates a new date object with the specified year, month and date.
Parameters
Parameter | Type | Description |
---|---|---|
year | number | The year. |
monthIndex | number | The month (0-11). |
date | number | The day of the month. |
Returns
Date
The new date object.
Since
9.5.0
setMonth()
setMonth(
date
,month
):Date
Defined in: src/classes/DateLib.ts:521
Sets the month of the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to set the month on. |
month | number | The 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:532
Sets the year of the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The date to set the year on. |
year | number | The year to set. |
Returns
Date
The new date with the year set.
startOfBroadcastWeek()
startOfBroadcastWeek(
date
,dateLib
?):Date
Defined in: src/classes/DateLib.ts:542
Returns the start of the broadcast week for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
dateLib ? | DateLib | - |
Returns
Date
The start of the broadcast week.
startOfDay()
startOfDay(
date
):Date
Defined in: src/classes/DateLib.ts:555
Returns the start of the day for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
Returns
Date
The start of the day.
startOfISOWeek()
startOfISOWeek(
date
):Date
Defined in: src/classes/DateLib.ts:565
Returns the start of the ISO week for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
Returns
Date
The start of the ISO week.
startOfMonth()
startOfMonth(
date
):Date
Defined in: src/classes/DateLib.ts:575
Returns the start of the month for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
Returns
Date
The start of the month.
startOfWeek()
startOfWeek(
date
):Date
Defined in: src/classes/DateLib.ts:585
Returns the start of the week for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
Returns
Date
The start of the week.
startOfYear()
startOfYear(
date
):Date
Defined in: src/classes/DateLib.ts:597
Returns the start of the year for the given date.
Parameters
Parameter | Type | Description |
---|---|---|
date | Date | The original date. |
Returns
Date
The start of the year.
today()
today():
Date
Defined in: src/classes/DateLib.ts:180
Creates a new date object to the today's date.
Returns
Date
The new date object.
Since
9.5.0
Properties
Date
Date:
DateConstructor
Defined in: src/classes/DateLib.ts:172
Reference to the built-in Date constructor.
Deprecated
Use newDate()
or today()
.
isDate()
isDate: (
value
) =>value is Date
Defined in: src/classes/DateLib.ts:448
Checks if the given value is a Date object.
Parameters
Parameter | Type | Description |
---|---|---|
value | unknown | The 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:105
The options for the date library.
overrides?
readonly
optional
overrides:Partial
<DateLib
>
Defined in: src/classes/DateLib.ts:108
Overrides for the date library functions.