Skip to main content

Class: DateLib

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

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

Constructor

new DateLib(options?, overrides?): DateLib

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

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

Methods

formatNumber()

formatNumber(value): string

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

Format number using the custom numbering system.

Parameters

ParameterTypeDescription
valuenumberThe number to format.

Returns

string

The formatted number.

Since

9.5.0


newDate()

newDate(year, monthIndex, date): Date

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

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


today()

today(): Date

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

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

Returns

Date

The new date object.

Since

9.5.0

Properties

Common Helpers

format()

format: (date, formatStr, options?) => string

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

Formats the given date using the specified format string.

Parameters
ParameterTypeDescription
datestring | number | DateThe original date
formatStrstring-
options?FormatOptionsAn object with options
Returns

string

The formatted date string

Name

format

Alias

formatDate

Description

Return the formatted date string in the given format. The result may vary by locale.

⚠️ Please note that the format tokens differ from Moment.js and other libraries. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

The characters wrapped between two single quotes characters (') are escaped. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote. (see the last example)

Format of the string is based on Unicode Technical Standard #35: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table with a few additions (see note 7 below the table).

Accepted patterns:

UnitPatternResult examplesNotes
EraG..GGGAD, BC
GGGGAnno Domini, Before Christ2
GGGGGA, B
Calendar yeary44, 1, 1900, 20175
yo44th, 1st, 0th, 17th5,7
yy44, 01, 00, 175
yyy044, 001, 1900, 20175
yyyy0044, 0001, 1900, 20175
yyyyy...3,5
Local week-numbering yearY44, 1, 1900, 20175
Yo44th, 1st, 1900th, 2017th5,7
YY44, 01, 00, 175,8
YYY044, 001, 1900, 20175
YYYY0044, 0001, 1900, 20175,8
YYYYY...3,5
ISO week-numbering yearR-43, 0, 1, 1900, 20175,7
RR-43, 00, 01, 1900, 20175,7
RRR-043, 000, 001, 1900, 20175,7
RRRR-0043, 0000, 0001, 1900, 20175,7
RRRRR...3,5,7
Extended yearu-43, 0, 1, 1900, 20175
uu-43, 01, 1900, 20175
uuu-043, 001, 1900, 20175
uuuu-0043, 0001, 1900, 20175
uuuuu...3,5
Quarter (formatting)Q1, 2, 3, 4
Qo1st, 2nd, 3rd, 4th7
QQ01, 02, 03, 04
QQQQ1, Q2, Q3, Q4
QQQQ1st quarter, 2nd quarter, ...2
QQQQQ1, 2, 3, 44
Quarter (stand-alone)q1, 2, 3, 4
qo1st, 2nd, 3rd, 4th7
qq01, 02, 03, 04
qqqQ1, Q2, Q3, Q4
qqqq1st quarter, 2nd quarter, ...2
qqqqq1, 2, 3, 44
Month (formatting)M1, 2, ..., 12
Mo1st, 2nd, ..., 12th7
MM01, 02, ..., 12
MMMJan, Feb, ..., Dec
MMMMJanuary, February, ..., December2
MMMMMJ, F, ..., D
Month (stand-alone)L1, 2, ..., 12
Lo1st, 2nd, ..., 12th7
LL01, 02, ..., 12
LLLJan, Feb, ..., Dec
LLLLJanuary, February, ..., December2
LLLLLJ, F, ..., D
Local week of yearw1, 2, ..., 53
wo1st, 2nd, ..., 53th7
ww01, 02, ..., 53
ISO week of yearI1, 2, ..., 537
Io1st, 2nd, ..., 53th7
II01, 02, ..., 537
Day of monthd1, 2, ..., 31
do1st, 2nd, ..., 31st7
dd01, 02, ..., 31
Day of yearD1, 2, ..., 365, 3669
Do1st, 2nd, ..., 365th, 366th7
DD01, 02, ..., 365, 3669
DDD001, 002, ..., 365, 366
DDDD...3
Day of week (formatting)E..EEEMon, Tue, Wed, ..., Sun
EEEEMonday, Tuesday, ..., Sunday2
EEEEEM, T, W, T, F, S, S
EEEEEEMo, Tu, We, Th, Fr, Sa, Su
ISO day of week (formatting)i1, 2, 3, ..., 77
io1st, 2nd, ..., 7th7
ii01, 02, ..., 077
iiiMon, Tue, Wed, ..., Sun7
iiiiMonday, Tuesday, ..., Sunday2,7
iiiiiM, T, W, T, F, S, S7
iiiiiiMo, Tu, We, Th, Fr, Sa, Su7
Local day of week (formatting)e2, 3, 4, ..., 1
eo2nd, 3rd, ..., 1st7
ee02, 03, ..., 01
eeeMon, Tue, Wed, ..., Sun
eeeeMonday, Tuesday, ..., Sunday2
eeeeeM, T, W, T, F, S, S
eeeeeeMo, Tu, We, Th, Fr, Sa, Su
Local day of week (stand-alone)c2, 3, 4, ..., 1
co2nd, 3rd, ..., 1st7
cc02, 03, ..., 01
cccMon, Tue, Wed, ..., Sun
ccccMonday, Tuesday, ..., Sunday2
cccccM, T, W, T, F, S, S
ccccccMo, Tu, We, Th, Fr, Sa, Su
AM, PMa..aaAM, PM
aaaam, pm
aaaaa.m., p.m.2
aaaaaa, p
AM, PM, noon, midnightb..bbAM, PM, noon, midnight
bbbam, pm, noon, midnight
bbbba.m., p.m., noon, midnight2
bbbbba, p, n, mi
Flexible day periodB..BBBat night, in the morning, ...
BBBBat night, in the morning, ...2
BBBBBat night, in the morning, ...
Hour [1-12]h1, 2, ..., 11, 12
ho1st, 2nd, ..., 11th, 12th7
hh01, 02, ..., 11, 12
Hour [0-23]H0, 1, 2, ..., 23
Ho0th, 1st, 2nd, ..., 23rd7
HH00, 01, 02, ..., 23
Hour [0-11]K1, 2, ..., 11, 0
Ko1st, 2nd, ..., 11th, 0th7
KK01, 02, ..., 11, 00
Hour [1-24]k24, 1, 2, ..., 23
ko24th, 1st, 2nd, ..., 23rd7
kk24, 01, 02, ..., 23
Minutem0, 1, ..., 59
mo0th, 1st, ..., 59th7
mm00, 01, ..., 59
Seconds0, 1, ..., 59
so0th, 1st, ..., 59th7
ss00, 01, ..., 59
Fraction of secondS0, 1, ..., 9
SS00, 01, ..., 99
SSS000, 001, ..., 999
SSSS...3
Timezone (ISO-8601 w/ Z)X-08, +0530, Z
XX-0800, +0530, Z
XXX-08:00, +05:30, Z
XXXX-0800, +0530, Z, +1234562
XXXXX-08:00, +05:30, Z, +12:34:56
Timezone (ISO-8601 w/o Z)x-08, +0530, +00
xx-0800, +0530, +0000
xxx-08:00, +05:30, +00:002
xxxx-0800, +0530, +0000, +123456
xxxxx-08:00, +05:30, +00:00, +12:34:56
Timezone (GMT)O...OOOGMT-8, GMT+5:30, GMT+0
OOOOGMT-08:00, GMT+05:30, GMT+00:002
Timezone (specific non-locat.)z...zzzGMT-8, GMT+5:30, GMT+06
zzzzGMT-08:00, GMT+05:30, GMT+00:002,6
Seconds timestampt51296957
tt...3,7
Milliseconds timestampT51296957
TT...3,7
Long localized dateP04/29/14537
PPApr 29, 14537
PPPApril 29th, 14537
PPPPFriday, April 29th, 14532,7
Long localized timep12:00 AM7
pp12:00:00 AM7
ppp12:00:00 AM GMT+27
pppp12:00:00 AM GMT+02:002,7
Combination of date and timePp04/29/1453, 12:00 AM7
PPppApr 29, 1453, 12:00:00 AM7
PPPpppApril 29th, 1453 at ...7
PPPPppppFriday, April 29th, 1453 at ...2,7
Notes:
  1. "Formatting" units (e.g. formatting quarter) in the default en-US locale are the same as "stand-alone" units, but are different in some languages. "Formatting" units are declined according to the rules of the language in the context of a date. "Stand-alone" units are always nominative singular:

    format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'

    format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'

  2. Any sequence of the identical letters is a pattern, unless it is escaped by the single quote characters (see below). If the sequence is longer than listed in table (e.g. EEEEEEEEEEE) the output will be the same as default pattern for this unit, usually the longest one (in case of ISO weekdays, EEEE). Default patterns for units are marked with "2" in the last column of the table.

    format(new Date(2017, 10, 6), 'MMM') //=> 'Nov'

    format(new Date(2017, 10, 6), 'MMMM') //=> 'November'

    format(new Date(2017, 10, 6), 'MMMMM') //=> 'N'

    format(new Date(2017, 10, 6), 'MMMMMM') //=> 'November'

    format(new Date(2017, 10, 6), 'MMMMMMM') //=> 'November'

  3. Some patterns could be unlimited length (such as yyyyyyyy). The output will be padded with zeros to match the length of the pattern.

    format(new Date(2017, 10, 6), 'yyyyyyyy') //=> '00002017'

  4. QQQQQ and qqqqq could be not strictly numerical in some locales. These tokens represent the shortest form of the quarter.

  5. The main difference between y and u patterns are B.C. years:

    Yearyu
    AC 111
    BC 110
    BC 22-1

    Also yy always returns the last two digits of a year, while uu pads single digit years to 2 characters and returns other years unchanged:

    Yearyyuu
    10101
    141414
    37676376
    1453531453

    The same difference is true for local and ISO week-numbering years (Y and R), except local week-numbering years are dependent on options.weekStartsOn and options.firstWeekContainsDate (compare getISOWeekYear and getWeekYear).

  6. Specific non-location timezones are currently unavailable in date-fns, so right now these tokens fall back to GMT timezones.

  7. These patterns are not in the Unicode Technical Standard #35:

    • i: ISO day of week
    • I: ISO week of year
    • R: ISO week-numbering year
    • t: seconds timestamp
    • T: milliseconds timestamp
    • o: ordinal number modifier
    • P: long localized date
    • p: long localized time
  8. YY and YYYY tokens represent week-numbering years but they are often confused with years. You should enable options.useAdditionalWeekYearTokens to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

  9. D and DD tokens represent days of the year but they are often confused with days of the month. You should enable options.useAdditionalDayOfYearTokens to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

Throws

date must not be Invalid Date

Throws

options.locale must contain localize property

Throws

options.locale must contain formatLong property

Throws

use yyyy instead of YYYY for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

Throws

use yy instead of YY for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

Throws

use d instead of D for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

Throws

use dd instead of DD for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md

Throws

format string contains an unescaped latin alphabet character

Examples
// Represent 11 February 2014 in middle-endian format:
const result = format(new Date(2014, 1, 11), 'MM/dd/yyyy')
//=> '02/11/2014'
// Represent 2 July 2014 in Esperanto:
import { eoLocale } from 'date-fns/locale/eo'
const result = format(new Date(2014, 6, 2), "do 'de' MMMM yyyy", {
locale: eoLocale
})
//=> '2-a de julio 2014'
// Escape string by single quote characters:
const result = format(new Date(2014, 6, 2, 15), "h 'o''clock'")
//=> "3 o'clock"
Param

The date to format.

Param

The format string.

Returns

The formatted date string.


isAfter()

isAfter: (date, dateToCompare) => boolean

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

Checks if the first date is after the second date.

Parameters
ParameterTypeDescription
datestring | number | DateThe date that should be after the other one to return true
dateToComparestring | number | DateThe date to compare with
Returns

boolean

The first date is after the second date

Name

isAfter

Description

Is the first date after the second one?

Example
// Is 10 July 1989 after 11 February 1987?
const result = isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11))
//=> true
Param

The date to compare.

Param

The date to compare with.

Returns

True if the first date is after the second date.


isBefore()

isBefore: (date, dateToCompare) => boolean

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

Checks if the first date is before the second date.

Parameters
ParameterTypeDescription
datestring | number | DateThe date that should be before the other one to return true
dateToComparestring | number | DateThe date to compare with
Returns

boolean

The first date is before the second date

Name

isBefore

Description

Is the first date before the second one?

Example
// Is 10 July 1989 before 11 February 1987?
const result = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11))
//=> false
Param

The date to compare.

Param

The date to compare with.

Returns

True if the first date is before the second date.


max()

max: <DateType, ResultDate>(dates, options?) => ResultDate

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

Returns the latest date in the given array of dates.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
datesDateArg<DateType>[]The dates to compare
options?MaxOptions<ResultDate>-
Returns

ResultDate

The latest of the dates

Name

max

Description

Return the latest of the given dates.

Example
// Which of these dates is the latest?
const result = max([
new Date(1989, 6, 10),
new Date(1987, 1, 11),
new Date(1995, 6, 2),
new Date(1990, 0, 1)
])
//=> Sun Jul 02 1995 00:00:00
Param

The array of dates to compare.

Returns

The latest date.


min()

min: <DateType, ResultDate>(dates, options?) => ResultDate

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

Returns the earliest date in the given array of dates.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
datesDateArg<DateType>[]The dates to compare
options?MinOptions<ResultDate>-
Returns

ResultDate

The earliest of the dates

Name

min

Description

Returns the earliest of the given dates.

Example
// Which of these dates is the earliest?
const result = min([
new Date(1989, 6, 10),
new Date(1987, 1, 11),
new Date(1995, 6, 2),
new Date(1990, 0, 1)
])
//=> Wed Feb 11 1987 00:00:00
Param

The array of dates to compare.

Returns

The earliest date.

Day Helpers

addDays()

addDays: <DateType, ResultDate>(date, amount, options?) => ResultDate

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

Adds the specified number of days to the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The date to be changed
amountnumberThe amount of days to be added.
options?AddDaysOptions<ResultDate>An object with options
Returns

ResultDate

The new date with the days added

Name

addDays

Description

Add the specified number of days to the given date.

Example
// Add 10 days to 1 September 2014:
const result = addDays(new Date(2014, 8, 1), 10)
//=> Thu Sep 11 2014 00:00:00
Param

The date to add days to.

Param

The number of days to add.

Returns

The new date with the days added.


differenceInCalendarDays()

differenceInCalendarDays: (laterDate, earlierDate, options?) => number

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

Returns the number of calendar days between the given dates.

Parameters
ParameterTypeDescription
laterDatestring | number | DateThe later date
earlierDatestring | number | DateThe earlier date
options?DifferenceInCalendarDaysOptionsThe options object
Returns

number

The number of calendar days

Name

differenceInCalendarDays

Description

Get the number of calendar days between the given dates. This means that the times are removed from the dates and then the difference in days is calculated.

Example
// How many calendar days are between
// 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
const result = differenceInCalendarDays(
new Date(2012, 6, 2, 0, 0),
new Date(2011, 6, 2, 23, 0)
)
//=> 366
// How many calendar days are between
// 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
const result = differenceInCalendarDays(
new Date(2011, 6, 3, 0, 1),
new Date(2011, 6, 2, 23, 59)
)
//=> 1
Param

The later date.

Param

The earlier date.

Returns

The number of calendar days between the dates.


isSameDay()

isSameDay: (laterDate, earlierDate, options?) => boolean

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

Checks if the given dates are on the same day.

Parameters
ParameterTypeDescription
laterDatestring | number | DateThe first date to check
earlierDatestring | number | DateThe second date to check
options?IsSameDayOptionsAn object with options
Returns

boolean

The dates are in the same day (and year and month)

Name

isSameDay

Description

Are the given dates in the same day (and year and month)?

Examples
// Are 4 September 06:00:00 and 4 September 18:00:00 in the same day?
const result = isSameDay(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 18, 0))
//=> true
// Are 4 September and 4 October in the same day?
const result = isSameDay(new Date(2014, 8, 4), new Date(2014, 9, 4))
//=> false
// Are 4 September, 2014 and 4 September, 2015 in the same day?
const result = isSameDay(new Date(2014, 8, 4), new Date(2015, 8, 4))
//=> false
Param

The first date to compare.

Param

The second date to compare.

Returns

True if the dates are on the same day.


startOfDay()

startOfDay: <DateType, ResultDate>(date, options?) => ResultDate

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

Returns the start of the day for the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?StartOfDayOptions<ResultDate>The options
Returns

ResultDate

The start of a day

Name

startOfDay

Description

Return the start of a day for the given date. The result will be in the local timezone.

Example
// The start of a day for 2 September 2014 11:55:00:
const result = startOfDay(new Date(2014, 8, 2, 11, 55, 0))
//=> Tue Sep 02 2014 00:00:00
Param

The original date.

Returns

The start of the day.

ISO Week Helpers

endOfISOWeek()

endOfISOWeek: <DateType, ResultDate>(date, options?) => ResultDate

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

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

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?EndOfISOWeekOptions<ResultDate>An object with options
Returns

ResultDate

The end of an ISO week

Name

endOfISOWeek

Description

Return the end of an ISO week for the given date. The result will be in the local timezone.

ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date

Example
// The end of an ISO week for 2 September 2014 11:55:00:
const result = endOfISOWeek(new Date(2014, 8, 2, 11, 55, 0))
//=> Sun Sep 07 2014 23:59:59.999
Param

The original date.

Returns

The end of the ISO week.


getISOWeek()

getISOWeek: (date, options?) => number

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

Returns the ISO week number for the given date.

Parameters
ParameterTypeDescription
datestring | number | DateThe given date
options?GetISOWeekOptionsThe options
Returns

number

The ISO week

Name

getISOWeek

Description

Get the ISO week of the given date.

ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date

Example
// Which week of the ISO-week numbering year is 2 January 2005?
const result = getISOWeek(new Date(2005, 0, 2))
//=> 53
Param

The date to get the ISO week number for.

Returns

The ISO week number.


startOfISOWeek()

startOfISOWeek: <DateType, ResultDate>(date, options?) => ResultDate

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

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

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?StartOfISOWeekOptions<ResultDate>An object with options
Returns

ResultDate

The start of an ISO week

Name

startOfISOWeek

Description

Return the start of an ISO week for the given date. The result will be in the local timezone.

ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date

Example
// The start of an ISO week for 2 September 2014 11:55:00:
const result = startOfISOWeek(new Date(2014, 8, 2, 11, 55, 0))
//=> Mon Sep 01 2014 00:00:00
Param

The original date.

Returns

The start of the ISO week.

Interval Helpers

eachMonthOfInterval()

eachMonthOfInterval: <IntervalType, Options>(interval, options?) => EachMonthOfIntervalResult<IntervalType, Options>

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

Returns the months between the given dates.

Type Parameters
Type ParameterDefault typeDescription
IntervalType extends Interval<DateArg<Date>, DateArg<Date>>-Interval type.
Options extends undefined | EachMonthOfIntervalOptions<Date>undefinedOptions type.
Parameters
ParameterTypeDescription
intervalIntervalTypeThe interval.
options?OptionsAn object with options.
Returns

EachMonthOfIntervalResult<IntervalType, Options>

The array with starts of months from the month of the interval start to the month of the interval end

Name

eachMonthOfInterval

Description

Return the array of months within the specified time interval.

Example
// Each month between 6 February 2014 and 10 August 2014:
const result = eachMonthOfInterval({
start: new Date(2014, 1, 6),
end: new Date(2014, 7, 10)
})
//=> [
// Sat Feb 01 2014 00:00:00,
// Sat Mar 01 2014 00:00:00,
// Tue Apr 01 2014 00:00:00,
// Thu May 01 2014 00:00:00,
// Sun Jun 01 2014 00:00:00,
// Tue Jul 01 2014 00:00:00,
// Fri Aug 01 2014 00:00:00
// ]
Param

The interval to get the months for.

Month Helpers

addMonths()

addMonths: <DateType, ResultDate>(date, amount, options?) => ResultDate

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

Adds the specified number of months to the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The date to be changed
amountnumberThe amount of months to be added.
options?AddMonthsOptions<ResultDate>The options object
Returns

ResultDate

The new date with the months added

Name

addMonths

Description

Add the specified number of months to the given date.

Example
// Add 5 months to 1 September 2014:
const result = addMonths(new Date(2014, 8, 1), 5)
//=> Sun Feb 01 2015 00:00:00

// Add one month to 30 January 2023:
const result = addMonths(new Date(2023, 0, 30), 1)
//=> Tue Feb 28 2023 00:00:00
Param

The date to add months to.

Param

The number of months to add.

Returns

The new date with the months added.


differenceInCalendarMonths()

differenceInCalendarMonths: (laterDate, earlierDate, options?) => number

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

Returns the number of calendar months between the given dates.

Parameters
ParameterTypeDescription
laterDatestring | number | DateThe later date
earlierDatestring | number | DateThe earlier date
options?DifferenceInCalendarMonthsOptionsAn object with options
Returns

number

The number of calendar months

Name

differenceInCalendarMonths

Description

Get the number of calendar months between the given dates.

Example
// How many calendar months are between 31 January 2014 and 1 September 2014?
const result = differenceInCalendarMonths(
new Date(2014, 8, 1),
new Date(2014, 0, 31)
)
//=> 8
Param

The later date.

Param

The earlier date.

Returns

The number of calendar months between the dates.


endOfMonth()

endOfMonth: <DateType, ResultDate>(date, options?) => ResultDate

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

Returns the end of the month for the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?EndOfMonthOptions<ResultDate>An object with options
Returns

ResultDate

The end of a month

Name

endOfMonth

Description

Return the end of a month for the given date. The result will be in the local timezone.

Example
// The end of a month for 2 September 2014 11:55:00:
const result = endOfMonth(new Date(2014, 8, 2, 11, 55, 0))
//=> Tue Sep 30 2014 23:59:59.999
Param

The original date.

Returns

The end of the month.


getMonth()

getMonth: (date, options?) => number

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

Returns the month of the given date.

Parameters
ParameterTypeDescription
datestring | number | DateThe given date
options?GetMonthOptionsAn object with options
Returns

number

The month index (0-11)

Name

getMonth

Description

Get the month of the given date.

Example
// Which month is 29 February 2012?
const result = getMonth(new Date(2012, 1, 29))
//=> 1
Param

The date to get the month for.

Returns

The month.


isSameMonth()

isSameMonth: (laterDate, earlierDate, options?) => boolean

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

Checks if the given dates are in the same month.

Parameters
ParameterTypeDescription
laterDatestring | number | DateThe first date to check
earlierDatestring | number | DateThe second date to check
options?IsSameMonthOptionsAn object with options
Returns

boolean

The dates are in the same month (and year)

Name

isSameMonth

Description

Are the given dates in the same month (and year)?

Examples
// Are 2 September 2014 and 25 September 2014 in the same month?
const result = isSameMonth(new Date(2014, 8, 2), new Date(2014, 8, 25))
//=> true
// Are 2 September 2014 and 25 September 2015 in the same month?
const result = isSameMonth(new Date(2014, 8, 2), new Date(2015, 8, 25))
//=> false
Param

The first date to compare.

Param

The second date to compare.

Returns

True if the dates are in the same month.


setMonth()

setMonth: <DateType, ResultDate>(date, month, options?) => ResultDate

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

Sets the month of the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The date to be changed
monthnumberThe month index to set (0-11)
options?SetMonthOptions<ResultDate>The options
Returns

ResultDate

The new date with the month set

Name

setMonth

Description

Set the month to the given date.

Example
// Set February to 1 September 2014:
const result = setMonth(new Date(2014, 8, 1), 1)
//=> Sat Feb 01 2014 00:00:00
Param

The date to set the month on.

Param

The month to set (0-11).

Returns

The new date with the month set.


startOfMonth()

startOfMonth: <DateType, ResultDate>(date, options?) => ResultDate

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

Returns the start of the month for the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?StartOfMonthOptions<ResultDate>An object with options
Returns

ResultDate

The start of a month

Name

startOfMonth

Description

Return the start of a month for the given date. The result will be in the local timezone.

Example
// The start of a month for 2 September 2014 11:55:00:
const result = startOfMonth(new Date(2014, 8, 2, 11, 55, 0))
//=> Mon Sep 01 2014 00:00:00
Param

The original date.

Returns

The start of the month.

Other

Date

Date: DateConstructor

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

Reference to the built-in Date constructor.

Deprecated

Use newDate() or today().


endOfBroadcastWeek()

endOfBroadcastWeek: (date, dateLib) => Date

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

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

Return the end date of the week in the broadcast calendar.

Parameters
ParameterType
dateDate
dateLibDateLib
Returns

Date

Since

9.4.0

Param

The original date.

Returns

The end of the broadcast week.


isDate()

isDate: (value) => value is Date

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

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:104

The options for the date library.


overrides?

readonly optional overrides: Partial<DateLib>

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

Overrides for the date library functions.


startOfBroadcastWeek()

startOfBroadcastWeek: (date, dateLib) => Date

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

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

Return the start date of the week in the broadcast calendar.

Parameters
ParameterType
dateDate
dateLibDateLib
Returns

Date

Since

9.4.0

Param

The original date.

Returns

The start of the broadcast week.

Week Helpers

addWeeks()

addWeeks: <DateType, ResultDate>(date, amount, options?) => ResultDate

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

Adds the specified number of weeks to the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The date to be changed
amountnumberThe amount of weeks to be added.
options?AddWeeksOptions<ResultDate>An object with options
Returns

ResultDate

The new date with the weeks added

Name

addWeeks

Description

Add the specified number of weeks to the given date.

Example
// Add 4 weeks to 1 September 2014:
const result = addWeeks(new Date(2014, 8, 1), 4)
//=> Mon Sep 29 2014 00:00:00
Param

The date to add weeks to.

Param

The number of weeks to add.

Returns

The new date with the weeks added.


endOfWeek()

endOfWeek: <DateType, ResultDate>(date, options?) => ResultDate

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

Returns the end of the week for the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?EndOfWeekOptions<ResultDate>An object with options
Returns

ResultDate

The end of a week

Name

endOfWeek

Description

Return the end of a week for the given date. The result will be in the local timezone.

Examples
// The end of a week for 2 September 2014 11:55:00:
const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0))
//=> Sat Sep 06 2014 23:59:59.999
// If the week starts on Monday, the end of the week for 2 September 2014 11:55:00:
const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
//=> Sun Sep 07 2014 23:59:59.999
Param

The original date.

Returns

The end of the week.


getWeek()

getWeek: (date, options?) => number

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

Returns the local week number for the given date.

Parameters
ParameterTypeDescription
datestring | number | DateThe given date
options?GetWeekOptionsAn object with options
Returns

number

The week

Name

getWeek

Description

Get the local week index of the given date. The exact calculation depends on the values of options.weekStartsOn (which is the index of the first day of the week) and options.firstWeekContainsDate (which is the day of January, which is always in the first week of the week-numbering year)

Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system

Examples
// Which week of the local week numbering year is 2 January 2005 with default options?
const result = getWeek(new Date(2005, 0, 2))
//=> 2
// Which week of the local week numbering year is 2 January 2005,
// if Monday is the first day of the week,
// and the first week of the year always contains 4 January?
const result = getWeek(new Date(2005, 0, 2), {
weekStartsOn: 1,
firstWeekContainsDate: 4
})
//=> 53
Param

The date to get the week number for.

Returns

The week number.


startOfWeek()

startOfWeek: <DateType, ResultDate>(date, options?) => ResultDate

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

Returns the start of the week for the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?StartOfWeekOptions<ResultDate>An object with options
Returns

ResultDate

The start of a week

Name

startOfWeek

Description

Return the start of a week for the given date. The result will be in the local timezone.

Examples
// The start of a week for 2 September 2014 11:55:00:
const result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0))
//=> Sun Aug 31 2014 00:00:00
// If the week starts on Monday, the start of the week for 2 September 2014 11:55:00:
const result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
//=> Mon Sep 01 2014 00:00:00
Param

The original date.

Returns

The start of the week.

Year Helpers

addYears()

addYears: <DateType, ResultDate>(date, amount, options?) => ResultDate

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

Adds the specified number of years to the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The date to be changed
amountnumberThe amount of years to be added.
options?AddYearsOptions<ResultDate>The options
Returns

ResultDate

The new date with the years added

Name

addYears

Description

Add the specified number of years to the given date.

Example
// Add 5 years to 1 September 2014:
const result = addYears(new Date(2014, 8, 1), 5)
//=> Sun Sep 01 2019 00:00:00
Param

The date to add years to.

Param

The number of years to add.

Returns

The new date with the years added.


endOfYear()

endOfYear: <DateType, ResultDate>(date, options?) => ResultDate

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

Returns the end of the year for the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?EndOfYearOptions<ResultDate>The options
Returns

ResultDate

The end of a year

Name

endOfYear

Description

Return the end of a year for the given date. The result will be in the local timezone.

Example
// The end of a year for 2 September 2014 11:55:00:
const result = endOfYear(new Date(2014, 8, 2, 11, 55, 0))
//=> Wed Dec 31 2014 23:59:59.999
Param

The original date.

Returns

The end of the year.


getYear()

getYear: (date, options?) => number

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

Returns the year of the given date.

Parameters
ParameterTypeDescription
datestring | number | DateThe given date
options?GetYearOptionsAn object with options
Returns

number

The year

Name

getYear

Description

Get the year of the given date.

Example
// Which year is 2 July 2014?
const result = getYear(new Date(2014, 6, 2))
//=> 2014
Param

The date to get the year for.

Returns

The year.


isSameYear()

isSameYear: (laterDate, earlierDate, options?) => boolean

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

Checks if the given dates are in the same year.

Parameters
ParameterTypeDescription
laterDatestring | number | DateThe first date to check
earlierDatestring | number | DateThe second date to check
options?IsSameYearOptionsAn object with options
Returns

boolean

The dates are in the same year

Name

isSameYear

Description

Are the given dates in the same year?

Example
// Are 2 September 2014 and 25 September 2014 in the same year?
const result = isSameYear(new Date(2014, 8, 2), new Date(2014, 8, 25))
//=> true
Param

The first date to compare.

Param

The second date to compare.

Returns

True if the dates are in the same year.


setYear()

setYear: <DateType, ResultDate>(date, year, options?) => ResultDate

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

Sets the year of the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The date to be changed
yearnumberThe year of the new date
options?SetYearOptions<ResultDate>An object with options.
Returns

ResultDate

The new date with the year set

Name

setYear

Description

Set the year to the given date.

Example
// Set year 2013 to 1 September 2014:
const result = setYear(new Date(2014, 8, 1), 2013)
//=> Sun Sep 01 2013 00:00:00
Param

The date to set the year on.

Param

The year to set.

Returns

The new date with the year set.


startOfYear()

startOfYear: <DateType, ResultDate>(date, options?) => ResultDate

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

Returns the start of the year for the given date.

Type Parameters
Type ParameterDefault typeDescription
DateType extends Date-The Date type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like UTCDate.
ResultDate extends DateDateTypeThe result Date type, it is the type returned from the context function if it is passed, or inferred from the arguments.
Parameters
ParameterTypeDescription
dateDateArg<DateType>The original date
options?StartOfYearOptions<ResultDate>The options
Returns

ResultDate

The start of a year

Name

startOfYear

Description

Return the start of a year for the given date. The result will be in the local timezone.

Example
// The start of a year for 2 September 2014 11:55:00:
const result = startOfYear(new Date(2014, 8, 2, 11, 55, 00))
//=> Wed Jan 01 2014 00:00:00
Param

The original date.

Returns

The start of the year.