Options
All
  • Public
  • Public/Protected
  • All
Menu

@jetblack/date

Index

Enumerations

Classes Calendars

Classes Duration

Classes Other

Classes Timezone

Interfaces Other

Interfaces Timezone

Type aliases

Variables Calendars

Variables Timezone

Functions Anchors

Functions Arithmetic

Functions Calendars

Functions Comparisons

Functions Comparisons ```js import { isDateBefore } from '@jetblack/date' console.log(isDateBefore(new Date('2000-01-02'), new Date('2000-01-01'))) // returns false console.log(isDateBefore(new Date('2000-01-01'), new Date('2000-01-02'))) // returns true console.log(isDateBefore(new Date('2000-01-01'), new Date('2000-01-01'))) // returns false ```

Functions Comparisons ```js import { isDateEqual } '@jetblack/date' console.log(isDateEqual(new Date('2000-01-02'), new Date('2000-01-01'))) // returns false console.log(isDateEqual(new Date('2000-01-01'), new Date('2000-01-02'))) // returns false console.log(isDateEqual(new Date('2000-01-01'), new Date('2000-01-01'))) // returns true ```

Functions Differences

Functions Formatting

Functions Miscellaneous

Functions Other

Functions Ranges

Functions Timezone

Type aliases

DayPeriods: [string, string]

Day periods: morning, afternoon.

Days: [string, string, string, string, string, string, string]

The 7 days of the week

Months: [string, string, string, string, string, string, string, string, string, string, string, string]

The 12 months of the year

NameStyle: "narrow" | "short" | "long"

The Intl name style.

Periodicity: "daily" | "weekly" | "monthly" | "quarterly" | "yearly"

Calendars Variables

calWeekends: WeekendCalendar = ...

The default calendar where Saturday and Sunday are considered holidays.

Timezone Variables

tzLocal: LocalTimezone = ...

The local timezone.

tzUtc: UtcTimezone = ...

The timezone for UTC.

Anchors Functions

  • endOfDay(date: Date, tz?: Timezone): Date
  • Return the end of the day for the given date.

    import { endOfDay, tzUtc } from '@jetblack/date'

    const d1 = tzUtc.makeDate(2000, 1, 1)
    const d2 = endOfDay(d1, tzUtc)
    console.log(d2.toISOString())
    // 2000-02-01T23:59:59.999Z

    Parameters

    • date: Date

      The start date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A new date which is the end of the day.

  • endOfHour(date: Date): Date
  • Return the end of the hour for the given date.

    import { endOfHour, tzUtc } from '@jetblack/date'

    const d1 = tzUtc.makeDate(2000, 1, 1)
    const d2 = endOfHour(d1, tzUtc)
    console.log(d2.toISOString())
    // 2000-02-01T00:59:59.999Z

    Parameters

    • date: Date

      The start date.

    Returns Date

    A new date which is the end of the hour.

  • endOfMinute(date: Date): Date
  • Return the end of the minute for the given date.

    import { endOfMinute, tzUtc } from '@jetblack/date'

    const d1 = tzUtc.makeDate(2000, 1, 1)
    const d2 = endOfMinute(d1, tzUtc)
    console.log(d2.toISOString())
    // 2000-02-01T00:00:59.999Z

    Parameters

    • date: Date

      The start date.

    Returns Date

    A new date which is the end of the minute.

  • endOfMonth(date: Date, tz?: Timezone): Date
  • Calculate the last moment of the month.

    import { endOfMonth, tzUtc } from '@jetblack/date'

    const days1 = endOfMonth(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days1.toISOString())
    // 2022-03-31T23:59:59.999Z

    // Compare with lastDayOfMonth.
    const days2 = lastDayOfMonth(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days2.toISOString())
    // 2022-03-31T00:00:00.000Z

    Parameters

    • date: Date

      The start date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A date which is the last day of the month for the given year and month.

  • endOfQuarter(date: Date, tz?: Timezone): Date
  • Find the last moment of the quarter.

    Parameters

    • date: Date

      A date

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The last moment of the quarter.

  • endOfSecond(date: Date): Date
  • Return the end of the second for the given date.

    import { endOfSecond, tzUtc } from '@jetblack/date'

    const d1 = tzUtc.makeDate(2000, 1, 1)
    const d2 = endOfSecond(d1, tzUtc)
    console.log(d2.toISOString())
    // 2000-02-01T00:00:00.999Z

    Parameters

    • date: Date

      The start date.

    Returns Date

    A new date which is the end of the second.

  • The end of today.

    Parameters

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The end of the current day.

  • endOfWeek(date: Date, tz?: Timezone): Date
  • Find the last moment of the week for a given date.

    Weeks start on Sunday and end on Saturday.

    import { endOfWeek, tzUtc } from '@jetblack/date'

    const days1 = endOfWeek(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days1.toISOString())
    // 2022-03-26T23:59:59.999Z

    // Compare to lastDayOfWeek
    const days2 = lastDayOfWeek(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days2.toISOString())
    // 2022-03-26T00:00:00.000Z

    Parameters

    • date: Date

      A date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The end of the week.

  • endOfWeekday(date: Date, weekStartsOn: number, tz?: Timezone): Date
  • Find the last moment of the week for a given date and first day of week.

    import { endOfWeekday, tzUtc } from '@jetblack/date'

    const days1 = endOfWeekday(new Date('2022-03-25T00:00:00Z'), 1, tzUtc)
    console.log(days1.toISOString())
    // 2022-03-27T23:59:59.999Z

    // Compare to lastDayOfWeekday
    const days2 = lastDayOfWeekday(new Date('2022-03-25T00:00:00Z'), 1, tzUtc)
    console.log(days2.toISOString())
    // 2022-03-27T00:00:00.000Z

    Parameters

    • date: Date

      A date.

    • weekStartsOn: number

      The first day of the week where 0 is Sunday.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The end of the week.

  • endOfYear(date: Date, tz?: Timezone): Date
  • Find the last moment of the year.

    import { endOfYear, tzUtc } from '@jetblack/date'

    const days1 = endOfYear(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days1.toISOString())
    // 2022-12-31T23:59:59.999Z

    // Compare with lastDayOfYear.
    const days2 = lastDayOfYear(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days2.toISOString())
    // 2022-12-31T00:00:00.000Z

    Parameters

    • date: Date

      A date

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The last moment of the year.

  • lastDayOfMonth(date: Date, tz?: Timezone): Date
  • Find the last day of the month.

    import { lastDayOfMonth, tzUtc } from '@jetblack/date'

    const days2 = lastDayOfMonth(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days2.toISOString())
    // 2022-03-31T00:00:00.000Z

    // Compare with endOfMonth.
    const days1 = endOfMonth(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days1.toISOString())
    // 2022-03-31T23:59:59.999Z

    Parameters

    • date: Date

      The start date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A date which is the last day of the month.

  • lastDayOfQuarter(date: Date, tz?: Timezone): Date
  • Find the last day of the quarter.

    import { lastDayOfQuarter, tzLocal } from '@jetblack/date'

    console.log(
    tzLocal.toISOString(lastDayOfQuarter(tzLocal.makeDate(2000, 1, 2), tzLocal))
    )
    // returns 2000-03-31T00:00:00-??:??

    Parameters

    • date: Date

      The start date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A date which is the last day of the quarter.

  • lastDayOfWeek(date: Date, tz?: Timezone): Date
  • Find the last day of the week for a given date.

    Note: weeks start on Sunday and end on Saturday.

    import { lastDayOfWeek, tzUtc } from '@jetblack/date'

    const days2 = lastDayOfWeek(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days2.toISOString())
    // 2022-03-26T00:00:00.000Z

    // Compare to endOfWeek
    const days1 = endOfWeek(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days1.toISOString())
    // 2022-03-26T23:59:59.999Z

    Parameters

    • date: Date

      A date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A date which is the last day of the week.

  • lastDayOfWeekday(date: Date, weekStartsOn: number, tz?: Timezone): Date
  • Find the last day of the week for a given date and first day of week.

    import { endOfWeekday, lastDayOfWeekday, tzUtc } from '@jetblack/date'

    const days2 = lastDayOfWeekday(new Date('2022-03-25T00:00:00Z'), 1, tzUtc)
    console.log(days2.toISOString())
    // 2022-03-27T00:00:00.000Z

    // Compare to endOfWeekday
    const days1 = endOfWeekday(new Date('2022-03-25T00:00:00Z'), 1, tzUtc)
    console.log(days1.toISOString())
    // 2022-03-27T23:59:59.999Z

    Parameters

    • date: Date

      A date.

    • weekStartsOn: number

      The first day of the week where 0 is Sunday.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A date which is the last day of the week.

  • lastDayOfYear(date: Date, tz?: Timezone): Date
  • Find the last day of the year.

    import { endOfYear, lastDayOfYear, tzUtc } from '@jetblack/date'

    const days2 = lastDayOfYear(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days2.toISOString())
    // 2022-12-31T00:00:00.000Z

    // Compare with endOfYear.
    const days1 = endOfYear(new Date('2022-03-25T00:00:00Z'), tzUtc)
    console.log(days1.toISOString())
    // 2022-12-31T23:59:59.999Z

    Parameters

    • date: Date

      The start date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A date which is the last day of the year.

  • startOfCurrentMonth(tz?: Timezone): Date
  • Find the start of the current month.

    Parameters

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the current month.

  • Find the start of the current year.

    Parameters

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the current year.

  • startOfDay(date: Date, tz?: Timezone): Date
  • Find the start of the day.

    import { startOfDay, tzLocal } from '@jetblack/date'

    const date = tzLocal.makeDate(2000, 0, 1, 10, 30)
    console.log(tzLocal.toISOString(startOfDay(date, tzLocal)))
    // returns "2000-01-01T00:00:00+??:??"

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the day.

  • startOfHour(date: Date): Date
  • Find the start of the hour.

    Parameters

    • date: Date

      The date.

    Returns Date

    The start of the hour.

  • startOfISOWeek(date: Date, tz?: Timezone): Date
  • Find the start of an ISO week for a given date.

    ISO weeks start on a Monday.

    Parameters

    • date: Date

      A date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the week.

  • startOfMinute(date: Date): Date
  • Find the start of the minute.

    Parameters

    • date: Date

      The date.

    Returns Date

    The start of the minute.

  • startOfMonth(date: Date, tz?: Timezone): Date
  • Find the start of the month for a given date.

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the month.

  • startOfQuarter(date: Date, tz?: Timezone): Date
  • Find the start of the quarter for a given date.

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the quarter.

  • startOfSecond(date: Date): Date
  • Find the start of the second.

    Parameters

    • date: Date

      The date.

    Returns Date

    The start of the second.

  • Find the start of today.

    Parameters

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of today.

  • startOfWeek(date: Date, tz?: Timezone): Date
  • Find the start of the week for a given date.

    Parameters

    • date: Date

      A date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the week.

  • startOfWeekYear(date: Date, tz?: Timezone): Date
  • Find the first week of the year for the week year of a given date.

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The date of the first week of the year.

  • startOfWeekday(date: Date, weekStartsOn: number, tz?: Timezone): Date
  • Find the start of the week for a given date and first day of week.

    Parameters

    • date: Date

      A date.

    • weekStartsOn: number

      The first day of the week where 0 is Sunday.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the week.

  • startOfYear(date: Date, tz?: Timezone): Date
  • Find the start of the year for a given date.

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the year.

Arithmetic Functions

  • Add business days to a date (or subtract if negative).

    If a calendar is not specified the calWeekends calendar is used.

    import { addBusinessDays } from '@jetblack/date'

    // Fri 7 Jan 2000
    const date = addBusinessDays(new Date('2000-01-07T00:00:00Z'), 1)
    console.log(date.getTime() === new Date('2000-01-10T00:00:00Z').getTime())

    Parameters

    • date: Date

      The start date.

    • count: number

      The number of days to add (or subtract if negative).

    • cal: Calendar = calWeekends

      The calendar to use to identify dates which are holidays. Defaults to the calWeekends calendar.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A new date adjusted by business days.

  • addDays(date: Date, numberOfDays: number, tz?: Timezone): Date
  • Add days to a date (or subtract if negative)

    Daylight savings time (DST) presents an issue for date arithmetic.

    The following example adds three days to the Saturday before the clocks change using the local timezone (London) and UTC (which does not have DST).

    When using the local timezone the "naturally correct" answer is returned. However, the elapsed time will be an hour less than when using UTC.

    Keeping the time change constant (as with UTC) can be useful when plotting data, or doing time series calculations (rolling averages, resampling, etc.).

    import { addDays, tzLocal, tzUtc } from '@jetblack/date'

    // The local timezone here is London.
    // In London on Sunday March 26 2000 the clocks go forward 1 hour.
    const d1 = new Date('2000-03-25T12:00:00')
    const d2 = addDays(d1, 3, tzLocal)
    const d3 = addDays(d1, 3, tzUtc)
    console.log(d1.toString()) // Sat Mar 25 2000 12:00:00 GMT+0000 (Greenwich Mean Time)
    console.log(d2.toString()) // Tue Mar 28 2000 12:00:00 GMT+0100 (British Summer Time)
    console.log(d3.toString()) // Tue Mar 28 2000 13:00:00 GMT+0100 (British Summer Time)

    Parameters

    • date: Date

      The start date.

    • numberOfDays: number

      The number of days to add (or subtract if negative).

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A new date adjusted by the number of days.

  • Add a duration to a date.

    import { Duration, tzLocal } from '@jetblack/date'

    const duration = new Duration('P1DT3H')
    const date = addDuration(tzLocal.makeDate(2000, 0, 1), duration, tzLocal)

    Parameters

    • date: Date

      The start date.

    • duration: Duration

      The duration to add.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A new date adjusted by adding the duration.

  • addHours(date: Date, numberOfHours: number): Date
  • Add hours to a date (or subtract if negative).

    Daylight savings time is handled appropriately.

    import { addHours } from '@jetblack/date'

    // The local timezone here is London.
    // In London on Sunday March 26 2000 the clocks go forward 1 hour.
    const d1 = new Date('2000-03-26T00:00:00')
    console.log(d1)
    // Sun Mar 26 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
    const d2 = addHours(d1, 1)
    console.log(d2)
    // Sun Mar 26 2000 02:00:00 GMT+0100 (British Summer Time)

    Parameters

    • date: Date

      The start date.

    • numberOfHours: number

      The number of hours to add (or subtract if negative).

    Returns Date

    A new date adjusted by the number of hours.

  • addMilliseconds(date: Date, numberOfMilliseconds: number): Date
  • Adds milliseconds to a date (or subtracts if negative).

    import { addMilliseconds } from '@jetblack/date'

    const d1 = addMilliseconds(new Date('2000-01-01T00:00:00.000'), 111)
    console.log(d1.toISOString())
    // 2000-01-01T00:00:00.111Z

    Parameters

    • date: Date

      The start date.

    • numberOfMilliseconds: number

      The number of milliseconds to add (or subtract if negative).

    Returns Date

    A new date adjusted by the number of milliseconds.

  • addMinutes(date: Date, numberOfMinutes: number): Date
  • Adds minutes to a date (or subtracts if negative).

    import { addMinutes } from '@jetblack/date'

    const d1 = addMinutes(new Date('2000-01-01T00:00:00.000'), 5)
    console.log(d1.toISOString())
    // 2000-01-01T00:05:00.000Z

    Parameters

    • date: Date

      The start date.

    • numberOfMinutes: number

      The number of minutes to ad (or subtract if negative).

    Returns Date

    A new date adjusted by the number of minutes.

  • addMonths(date: Date, numberOfMonths: number, tz?: Timezone): Date
  • Add months to a date (or subtract if negative).

    The day of the month is kept constant if possible. Where the destination has less days at the end of the month, the surplus days are added.

    import { addMonths, tzUtc } from '@jetblack/date'

    // The day of the month is kept constant if possible.
    const d1 = new Date('2000-01-12')
    console.log(d1.toString(), tzUtc)
    // Wed Jan 12 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
    const d2 = addMonths(d1, 1, tzUtc)
    console.log(d2.toString())
    // Sat Feb 12 2000 00:00:00 GMT+0000 (Greenwich Mean Time)

    // If there are less days in the following month the surplus days are added.
    const d3 = new Date('2000-01-31')
    console.log(d3.toString())
    // Mon Jan 31 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
    const d4 = addMonths(d3, 1, tzUtc)
    console.log(d4.toString())
    // Thu Mar 02 2000 00:00:00 GMT+0000 (Greenwich Mean Time)

    Parameters

    • date: Date

      The start date.

    • numberOfMonths: number

      The number of months to add (or subtract if negative)

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A new date adjusted by the number of months.

  • addNthDayOfWeek(date: Date, nth: number, dow: number, strictlyDifferent: boolean, tz?: Timezone): Date
  • Add or subtract days of the week.

    If the start date lies on the specified day of the week and the strictly different flag is false, the current date would be considered the first day of the week.

    Note that the JavaScript Date object treats 0 as Sunday and 6 as Saturday.

    import { addNthDayOfWeek } from '@jetblack/date'

    // Saturday 1 January 1990
    const d1 = new Date('2000-01-01T00:00:00Z')

    // The third Thursday of the month.
    const d2 = addNthDayOfWeek(d1, 3, 4, false)
    console.log(d2)
    // Thu Jan 20 2000 00:00:00 GMT+0000 (Greenwich Mean Time)

    // The last Friday of the month
    const d3 = addNthDayOfWeek(endOfMonth(d1), -1, 5, false)
    console.log(d3)
    // Fri Jan 28 2000 00:00:00 GMT+0000 (Greenwich Mean Time)

    Parameters

    • date: Date

      The start date.

    • nth: number

      The number of week days.

    • dow: number

      The day of the week where Sunday is 0.

    • strictlyDifferent: boolean

      When true the returned date must be different to the start date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The nth week day.

  • addQuarters(date: Date, numberOfQuarters: number, tz?: Timezone): Date
  • Add quarters to a date (or subtract if negative).

    The day of the quarter is kept constant if possible. Where the destination has less days at the end of the month, the surplus days are added.

    Parameters

    • date: Date

      The start date.

    • numberOfQuarters: number

      The number of quarters to add (or subtract if negative)

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A new date adjusted by the number of quarters.

  • addSeconds(date: Date, numberOfSeconds: number): Date
  • Adds seconds to a date (or subtracts if negative).

    import { addSeconds } from '@jetblack/date'

    const d1 = addSeconds(new Date('2000-01-01T00:00:00.000'), 61)
    console.log(d1.toISOString())
    // 2000-01-01T00:01:01.000Z

    Parameters

    • date: Date

      The start date.

    • numberOfSeconds: number

      The number of seconds to add.

    Returns Date

    A new date adjusted by the number of seconds.

  • addYears(date: Date, numberOfYears: number, tz?: Timezone): Date
  • Add years to a date (or subtract if negative).

    Note how surplus days for a leap year are added.

    import { addYears, tzUtc } from '@jetblack/date'

    // There is no 29 February in 2001.
    const d1 = addYears(new Date('2000-02-29'), 1, tzUtc)
    console.log(d1.toString())
    // Thu Mar 01 2001 00:00:00 GMT+0000 (Greenwich Mean Time)

    // 2004 is also a leap year.
    const d2 = addYears(new Date('2000-02-29'), 4, tzUtc)
    console.log(d2.toString())
    // Sun Feb 29 2004 00:00:00 GMT+0000 (Greenwich Mean Time)

    Parameters

    • date: Date

      The start date.

    • numberOfYears: number

      The number of years to add (or subtract if negative).

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A new date adjusted by the number of years.

  • Subtract a duration from a date.

    deprecated

    This function was required before duration could be negative and is no longer necessary. It will be removed in the next major release.

    Parameters

    • date: Date

      The start date.

    • duration: Duration

      The duration to subtract.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    A new date adjusted by subtracting the duration.

Calendars Functions

  • daysInMonth(year: number, monthIndex: number): number
  • Find the number of days in a month for a given year.

    import { daysInMonth } from '@jetblack/date'

    console.log(daysInMonth(2000, 0))
    // 31

    console.log(daysInMonth(2000, 1))
    // 29

    console.log(daysInMonth(2001, 1))
    // 28

    Parameters

    • year: number

      The year.

    • monthIndex: number

      The month index where January is 0.

    Returns number

    The number of days in the month for the given year.

  • daysInYear(year: number): number
  • Find the number of days in a year.

    import { daysInYear } from '@jetblack/date'

    console.log(daysInYear(2000))
    // 366

    console.log(daysInYear(2001))
    // 365

    Parameters

    • year: number

      The year.

    Returns number

    The number of days in the year.

  • easter(year: number, tz?: Timezone): Date
  • Calculates Easter in the Gregorian calendar.

    Parameters

    • year: number

      The year.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The date for easter in the given year.

  • isLastDayOfMonth(date: Date, tz?: Timezone): boolean
  • Check if the date is the last day of the month.

    The time component is ignored.

    Parameters

    • date: Date

      The date to check.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns boolean

    True if the date is the last day of the month.

  • isLastDayOfQuarter(date: Date, tz?: Timezone): boolean
  • Check if the date is the last day of the quarter.

    Any time component is ignored.

    import { tzLocal, isLastDayOfQuarter } from '@jetblack/date'

    const jan30 = tzLocal.makeDate(2008, 0, 30)
    console.log(isLastDayOfQuarter(jan30, tzLocal))
    // returns false

    const mar31 = tzLocal.makeDate(2008, 2, 31)
    console.log(isLastDayOfQuarter(mar31, tzLocal))
    // returns true

    Parameters

    • date: Date

      The date to check.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns boolean

    True if the date is the last day of the quarter.

  • isLeapYear(year: number): boolean
  • Check if the year is a leap year.

    import { isLeapYear } from '@jetblack/date'

    // 2000 was a century leap year.
    console.log(isLeapYear(2000) === true)

    // 1900 was not.
    console.log(isLeapYear(1900) !== true)

    Parameters

    • year: number

      The year.

    Returns boolean

    True if the year is a leap year, otherwise false.

  • quarterOfYear(date: Date, tz?: Timezone): number
  • Find the quarter for a given date.

    import { quarterOfYear } from '@jetblack/date'

    console.log(quarterOfYear(new Date('2000-04-01')))
    // 2

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The quarter of the year.

Comparisons Functions

  • areInSameQuarter(first: Date, second: Date, tz?: Timezone): boolean
  • Compare two dates to see if they are in the same quarter of the year.

    import { areInSameQuarter } from '@jetblack/date'

    console.log(areInSameQuarter(new Date('2000-01-01'), new Date('2000-02-01')))
    // true
    console.log(areInSameQuarter(new Date('2000-01-01'), new Date('2000-04-01')))
    // false

    Parameters

    • first: Date

      The first date.

    • second: Date

      The second date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns boolean

    True if the dates are in the same quarter, otherwise false.

  • compareDateAsc(a: Date, b: Date): number
  • Compares two dates ascending.

    import { compareDateAsc } from '@jetblack/date'

    const a = [
    new Date('2000-07-01'),
    new Date('2000-05-01'),
    new Date('2000-06-01')
    ]
    console.log(a.sort(compareDateAsc))
    // 0: Mon May 01 2000 01:00:00 GMT+0100 (British Summer Time)
    // 1: Thu Jun 01 2000 01:00:00 GMT+0100 (British Summer Time)
    // 2: Sat Jul 01 2000 01:00:00 GMT+0100 (British Summer Time)

    Parameters

    • a: Date

      The first date.

    • b: Date

      The second date.

    Returns number

    1 if the first date is greater than the second date, -1 if the first date is less than the last date, otherwise 0.

  • compareDateDesc(a: Date, b: Date): number
  • Compares two dates descending.

    import { compareDateDesc } from '@jetblack/date'

    const a = [
    new Date('2000-07-01'),
    new Date('2000-05-01'),
    new Date('2000-06-01')
    ]
    console.log(a.sort(compareDateDesc))
    // 0: Sat Jul 01 2000 01:00:00 GMT+0100 (British Summer Time)
    // 1: Thu Jun 01 2000 01:00:00 GMT+0100 (British Summer Time)
    // 2: Mon May 01 2000 01:00:00 GMT+0100 (British Summer Time)

    Parameters

    • a: Date

      The first date.

    • b: Date

      The second date.

    Returns number

    1 if the second date is greater than the first date, -1 if the second date is less than the first date, otherwise 0.

  • isDateAfter(lhs: Date, rhs: Date): boolean
  • Checks if the left date is after the right date.

    import { isDateAfter } from '@jetblack/date'

    console.log(isDateAfter(new Date('2000-01-02'), new Date('2000-01-01')))
    // returns true
    console.log(isDateAfter(new Date('2000-01-01'), new Date('2000-01-02')))
    // returns false
    console.log(isDateAfter(new Date('2000-01-01'), new Date('2000-01-01')))
    // returns false

    Parameters

    • lhs: Date

      The left date.

    • rhs: Date

      The right date.

    Returns boolean

    True if the left date is after the right date, otherwise false.

  • isDateNotEqual(lhs: Date, rhs: Date): boolean
  • Checks if the dates are not equal.

    import { isDateNotEqual } from '@jetblack/date'

    console.log(isDateNotEqual(new Date('2000-01-02'), new Date('2000-01-01')))
    // returns true
    console.log(isDateNotEqual(new Date('2000-01-01'), new Date('2000-01-02')))
    // returns true
    console.log(isDateNotEqual(new Date('2000-01-01'), new Date('2000-01-01')))
    // returns false

    Parameters

    • lhs: Date

      The left date.

    • rhs: Date

      The right date.

    Returns boolean

    True if the dates are not equal, otherwise false.

  • isDateOnOrAfter(lhs: Date, rhs: Date): boolean
  • Checks if the left date is on or after the right date.

    import { isDateOnOrAfter } from '@jetblack/date'

    console.log(isDateOnOrAfter(new Date('2000-01-02'), new Date('2000-01-01')))
    // returns true
    console.log(isDateOnOrAfter(new Date('2000-01-01'), new Date('2000-01-02')))
    // returns false
    console.log(isDateOnOrAfter(new Date('2000-01-01'), new Date('2000-01-01')))
    // returns true

    Parameters

    • lhs: Date

      The left date.

    • rhs: Date

      The right date.

    Returns boolean

    True if the left date is on or after the right date, otherwise false.

  • isDateOnOrBefore(lhs: Date, rhs: Date): boolean
  • Checks if the left date is on or before the right date.

    import { isDateOnOrBefore } from '@jetblack/date'

    console.log(isDateOnOrBefore(new Date('2000-01-02'), new Date('2000-01-01')))
    // returns false
    console.log(isDateOnOrBefore(new Date('2000-01-01'), new Date('2000-01-02')))
    // returns true
    console.log(isDateOnOrBefore(new Date('2000-01-01'), new Date('2000-01-01')))
    // returns true

    Parameters

    • lhs: Date

      The left date.

    • rhs: Date

      The right date.

    Returns boolean

    True if the left date is on or before the right date, otherwise false.

  • maxDate(...dates: Date[]): Date
  • Find the largest date.

    import { maxDate } from '@jetblack/date'

    const d1 = maxDate(
    new Date('2000-01-31T00:00:00Z'),
    new Date('2000-01-01T00:00:00Z'),
    new Date('2000-01-10T00:00:00Z')
    )
    console.log(d1.toISOString())
    // 2000-01-31T00:00:00.000Z

    const dates = [
    new Date('2000-01-31T00:00:00Z'),
    new Date('2000-01-01T00:00:00Z'),
    new Date('2000-01-10T00:00:00Z')
    ]
    const d2 = maxDate(...dates) // use the spread operator for an array.
    console.log(d2.toISOString())
    // 2000-01-31T00:00:00.000Z

    Parameters

    • Rest ...dates: Date[]

      The dates to check.

    Returns Date

    The largest date.

  • minDate(...dates: Date[]): Date
  • Find the smallest date.

    import { minDate } from '@jetblack/date'

    const d1 = minDate(
    new Date('2000-01-31T00:00:00Z'),
    new Date('2000-01-01T00:00:00Z'),
    new Date('2000-01-10T00:00:00Z')
    )
    console.log(d1.toISOString())
    // 2000-01-01T00:00:00.000Z

    const dates = [
    new Date('2000-01-31T00:00:00Z'),
    new Date('2000-01-01T00:00:00Z'),
    new Date('2000-01-10T00:00:00Z')
    ]
    const d2 = minDate(...dates) // use the spread operator for an array
    console.log(d2.toISOString())
    // 2000-01-01T00:00:00.000Z

    Parameters

    • Rest ...dates: Date[]

      The dates to check.

    Returns Date

    The smallest date.

Comparisons ```js import { isDateBefore } from '@jetblack/date' console.log(isDateBefore(new Date('2000-01-02'), new Date('2000-01-01'))) // returns false console.log(isDateBefore(new Date('2000-01-01'), new Date('2000-01-02'))) // returns true console.log(isDateBefore(new Date('2000-01-01'), new Date('2000-01-01'))) // returns false ``` Functions

  • isDateBefore(lhs: Date, rhs: Date): boolean
  • Checks if the left date is before the right date.

    Parameters

    • lhs: Date

      The left date.

    • rhs: Date

      The right date.

    Returns boolean

    True if the left date is before the right date, otherwise false.

Comparisons ```js import { isDateEqual } '@jetblack/date' console.log(isDateEqual(new Date('2000-01-02'), new Date('2000-01-01'))) // returns false console.log(isDateEqual(new Date('2000-01-01'), new Date('2000-01-02'))) // returns false console.log(isDateEqual(new Date('2000-01-01'), new Date('2000-01-01'))) // returns true ``` Functions

  • isDateEqual(lhs: Date, rhs: Date): boolean
  • Checks if the dates are equal.

    Parameters

    • lhs: Date

      The left date.

    • rhs: Date

      The right date.

    Returns boolean

    True if the left date equals the right date, otherwise false.

Differences Functions

  • diffInCalDays(leftDate: Date, rightDate: Date, tz?: Timezone): number
  • Find the number of whole days between two dates, discarding any time component.

    import { diffInCalDays, tzUtc } from '@jetblack/date'

    const days1 = diffInCalDays(
    new Date('2022-03-25T00:00:00Z'),
    new Date('2022-01-25T00:00:00Z'),
    tzUtc
    )
    console.log(days1)
    // 59

    // The time part is discarded.
    const days2 = diffInCalDays(
    new Date('2022-03-25T12:00:00Z'),
    new Date('2022-01-25T00:00:00Z'),
    tzUtc
    )
    console.log(days2)
    // 59

    Parameters

    • leftDate: Date

      The left date.

    • rightDate: Date

      The right date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The number of days between the start of day on the start and end dates.

  • diffInCalMonths(leftDate: Date, rightDate: Date, tz?: Timezone): number
  • Find the number of whole months between two dates.

    Parameters

    • leftDate: Date

      The left date.

    • rightDate: Date

      The right date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The number of months between two dates.

  • diffInCalYears(leftDate: Date, rightDate: Date, tz?: Timezone): number
  • Find the number of whole years between two dates.

    Parameters

    • leftDate: Date

      The left date.

    • rightDate: Date

      The right date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The number of years between two dates.

  • diffInDays(leftDate: Date, rightDate: Date): number
  • Find the number of days between two dates including the fractional component.

    import { diffInDays, tzUtc } from '@jetblack/date'

    const days1 = diffInDays(
    new Date('2022-03-25T00:00:00Z'),
    new Date('2022-01-25T00:00:00Z'),
    tzUtc
    )
    console.log(days1)
    // 59

    // The time part is retained.
    const days2 = diffInDays(
    new Date('2022-03-25T12:00:00Z'),
    new Date('2022-01-25T00:00:00Z'),
    tzUtc
    )
    console.log(days2)
    // 59.5

    Parameters

    • leftDate: Date

      The left date.

    • rightDate: Date

      The right date.

    Returns number

    The number of days between the start of day on the start and end dates.

  • diffInHours(leftDate: Date, rightDate: Date): number
  • Find the number of hours between two dates including fractional minutes.

    Parameters

    • leftDate: Date

      The left date.

    • rightDate: Date

      The right date.

    Returns number

    The number of days between the start of day on the start and end dates.

  • diffInMilliseconds(leftDate: Date, rightDate: Date): number
  • Find the number of milliseconds between two dates.

    Parameters

    • leftDate: Date

      The left date.

    • rightDate: Date

      The right date.

    Returns number

    The number of days milliseconds between the left and right date.

  • diffInMinutes(leftDate: Date, rightDate: Date): number
  • Find the number of minutes between two dates including fractional seconds.

    Parameters

    • leftDate: Date

      The left date.

    • rightDate: Date

      The right date.

    Returns number

    The number of days between the start of day on the start and end dates.

  • diffInSeconds(leftDate: Date, rightDate: Date): number
  • Find the number of seconds between two dates including fractional milliseconds.

    Parameters

    • leftDate: Date

      The left date.

    • rightDate: Date

      The right date.

    Returns number

    The number of days between the start of day on the start and end dates.

Formatting Functions

  • formatDate(date: Date, pattern?: string, tz?: Timezone, locale?: undefined | string | I18nSettings): string
  • Format a date with a pattern.

    import { formatDate } from '@jetblack/date'

    const d = new Date("2000-01-01")
    const s = formatDate(d, "d-mmm-yy")
    console.log(s)
    > 1-Jan-00

    Pattern items

    Value Description
    d Day of the month as digits; no leading zero for single-digit days.
    dd Day of the month as digits; leading zero for single-digit days.
    ddd Day of the week as the short representation (for en a three-letter abbreviation).
    dddd Day of the week as its full name.
    DD Day of the month with the plural suffix.
    DDD Day of the week as the narrow representation.
    m Month as digits; no leading zero for single-digit months.
    mm Month as digits; leading zero for single-digit months.
    mmm Month as a three-letter abbreviation.
    mmmm Month as its full name.
    yy Year as last two digits; leading zero for years less than 10.
    yyyy Year represented by four digits.
    h Hours; no leading zero for single-digit hours (12-hour clock).
    hh Hours; leading zero for single-digit hours (12-hour clock).
    H Hours; no leading zero for single-digit hours (24-hour clock).
    HH Hours; leading zero for single-digit hours (24-hour clock).
    M Minutes; no leading zero for single-digit minutes.
    MM Minutes; leading zero for single-digit minutes.
    S Seconds; no leading zero for single-digit seconds.
    SS Seconds; leading zero for single-digit seconds.
    F Hundreds of milliseconds.
    FF Tens of milliseconds with a leading zero for single-digit values.
    FFF Milliseconds: zero padded.
    t Narrow dayPeriod from locale.
    tt Short dayPeriod from locale.
    ttt Long dayPeriod from locale.
    Z Timezone name.
    o GMT/UTC timezone offset, e.g. -0500 or +0230.
    '...' or "..." Literal character sequence. Surrounding quotes are removed.

    Parameters

    • date: Date

      A date.

    • pattern: string = "yyyy-mm-dd'T'HH:MM:SSo"

      The format pattern.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    • locale: undefined | string | I18nSettings = undefined

      An optional locale. Defaults to the current browser locale.

    Returns string

    The formatted date string.

  • parseDate(text: string, format: string, tz?: Timezone, locale?: undefined | string | I18nSettings): Date | null
  • Parse a date string into a Javascript Date object.

    If the timezone offset is absent the dates will be according to the local timezone.

    import { parseDate } from '@jetblack/date'

    const d = parseDate('12 March, 1998', 'dd mmm, yyyy')

    Pattern items

    Value Description
    d Day of the month as digits; no leading zero for single-digit days.
    dd Day of the month as digits; leading zero for single-digit days.
    ddd Day of the week as a three-letter abbreviation.
    dddd Day of the week as its full name.
    DD Day of the month with the plural suffix.
    m Month as digits; no leading zero for single-digit months.
    mm Month as digits; leading zero for single-digit months.
    mmm Month as a three-letter abbreviation.
    mmmm Month as its full name.
    yy Year as last two digits; leading zero for years less than 10.
    yyyy Year represented by four digits.
    h Hours; no leading zero for single-digit hours (12-hour clock).
    hh Hours; leading zero for single-digit hours (12-hour clock).
    H Hours; no leading zero for single-digit hours (24-hour clock).
    HH Hours; leading zero for single-digit hours (24-hour clock).
    M Minutes; no leading zero for single-digit minutes.
    MM Minutes; leading zero for single-digit minutes.
    S Seconds; no leading zero for single-digit seconds.
    SS Seconds; leading zero for single-digit seconds.
    F Hundreds of milliseconds.
    FF Tens of milliseconds with a leading zero for single-digit values.
    FFF Milliseconds: zero padded.
    t Narrow dayPeriod from locale.
    tt Short dayPeriod from locale.
    ttt Long dayPeriod from locale.
    Z Timezone offset.
    [dhM] Literal characters.

    Parameters

    • text: string

      Date string

    • format: string

      Date parse format

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    • locale: undefined | string | I18nSettings = undefined

      An optional locale. Defaults to the current locale.

    Returns Date | null

    The date, or null if parsing failed.

Miscellaneous Functions

  • nearestBusinessDay(date: Date, preferForward?: boolean, cal?: Calendar, tz?: Timezone): Date
  • Find the nearest business date.

    Parameters

    • date: Date

      The start date.

    • preferForward: boolean = true

      If true a future date is preferred if both directions have the same cost. Defaults to true.

    • cal: Calendar = calWeekends

      The calendar to use to identify business days. Defaults to the weekend calendar.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The nearest business date.

  • weekOfYear(date: Date, tz?: Timezone): number
  • Find the week of the year for a given date.

    Weeks start on 1 January of any given year.

    import { weekOfYear, tzUtc } from '@jetblack/date'

    console.log(weekOfYear(new Date('1999-12-31T00:00:00Z'), tzUtc))
    // 53
    console.log(weekOfYear(new Date('2000-01-01T00:00:00Z'), tzUtc))
    // 1
    console.log(weekOfYear(new Date('2000-01-07T00:00:00Z'), tzUtc))
    // 1
    console.log(weekOfYear(new Date('2000-01-08T00:00:00Z'), tzUtc))
    // 2
    console.log(weekOfYear(new Date('2000-12-31T00:00:00Z'), tzUtc))
    // 53

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The week of the year.

  • weekYear(date: Date, tz?: Timezone): number
  • Find the week year for a given date.

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The week year.

Other Functions

  • Adjusts a non-business day to the appropriate nearest business day.

    import { adjustBusinessDay, BusinessDayConvention } from '@jetblack/date'

    const d1 = new Date('2000-01-01')
    console.log(d1)
    // Sat Jan 01 2000 00:00:00 GMT+0000 (Greenwich Mean Time)

    const d2 = adjustBusinessDay(d1, BusinessDayConvention.FOLLOWING)
    console.log(d2.toString())
    // Mon Jan 03 2000 00:00:00 GMT+0000 (Greenwich Mean Time)

    const d3 = adjustBusinessDay(d1, BusinessDayConvention.PRECEDING)
    console.log(d3.toString())
    // Fri Dec 31 1999 00:00:00 GMT+0000 (Greenwich Mean Time)

    Parameters

    • date: Date

      The date.

    • convention: BusinessDayConvention = BusinessDayConvention.FOLLOWING

      The business day conventions. Defaults to BusinessDayConvention.FOLLOWING.

    • prefer_forward: boolean = true

      If true prefer the nearest business day in the future. Defaults to true.

    • cal: Calendar = calWeekends

      An optional holiday calendar. Defaults to calWeekend.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The adjusted date.

  • dateFormat(date: Date, pattern?: string, tz?: Timezone, locale?: undefined | string | I18nSettings): string
  • dayOfYear(date: Date, tz?: Timezone): number
  • Find the day of the year.

    import { dayOfYear, tzUtc } from '@jetblack/date'

    const d = new Date('2020-07-12T00:00:00Z')
    const day = dayOfYear(d, tzUtc)
    console.log(day)
    // 94

    Parameters

    • date: Date

      The date

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The day of the year

  • findDayIndex(dates: Date[], day: number, tz?: Timezone): number
  • Find the index of the first date with the given day of the month.

    Parameters

    • dates: Date[]

      The dates

    • day: number

      The day of the month to check

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The index of the first match, or -1.

  • findEndOfMonthIndex(dates: Date[], tz?: Timezone): number
  • Find the index of the first date that is the end of the month.

    Parameters

    • dates: Date[]

      The dates

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The index of the first match, or -1.

  • findLastDayIndex(dates: Date[], day: number, tz?: Timezone): number
  • Find the index of the last date with the given day of the month.

    Parameters

    • dates: Date[]

      The dates

    • day: number

      The day of the month to check

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The index of the last match, or -1.

  • findLastEndOfMonthIndex(dates: Date[], tz?: Timezone): number
  • Find the index of the last date that is the end of the month.

    Parameters

    • dates: Date[]

      The dates

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The index of the last match, or -1.

  • findLastStartOfMonthIndex(dates: Date[], tz?: Timezone): number
  • Find the index of the last date that is the start of the month.

    Parameters

    • dates: Date[]

      The dates

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The index of the last match, or -1.

  • findLastWeekdayIndex(dates: Date[], weekday: number, tz?: Timezone): number
  • Find the index of the last date with the given weekday.

    Parameters

    • dates: Date[]

      The dates

    • weekday: number

      The weekday to check

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The index of the last match, or -1.

  • findStartOfMonthIndex(dates: Date[], tz?: Timezone): number
  • Find the index of the first date that is the start of the month.

    Parameters

    • dates: Date[]

      The dates

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The index of the first match, or -1.

  • findWeekdayIndex(dates: Date[], weekday: number, tz?: Timezone): number
  • Find the index of the first date with the given weekday.

    Parameters

    • dates: Date[]

      The dates

    • weekday: number

      The weekday to check

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The index of the first match, or -1.

  • isValidDate(value: any): boolean
  • Check if a value is a valid date.

    import { isValidDate } from '@jetblack/date'

    > const value = new Date("foo")
    > console.log(isValidDate(value))
    false

    Parameters

    • value: any

      The value to check.

    Returns boolean

    True if the value is a valid date.

  • isoWeekDate(date: Date, tz?: Timezone): number[]
  • Find the ISO week date.

    import { isoWeekDate } from '@jetblack/date'

    console.log(isoWeekDate(new Date("2000-01-01T00:00:00Z")))
    // [1999, 52, 6]

    Parameters

    • date: Date

      The date

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number[]

    The ISO date as a year, week, and day.

  • isoWeekOfYear(date: Date, tz?: Timezone): number
  • Find the ISO week of the year.

    Parameters

    • date: Date

      The date.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The ISO week.

  • leapSeconds(firstDate: Date, secondDate: Date): number
  • Find the number of leap seconds between the dates (inclusive).

    import { endOfYear, leapSeconds, tzUtc } from '@jetblack/date'

    const d1 = new Date('1972-01-01T00:00:00Z')
    const d2 = endOfYear(d1, tzUtc)
    // There were two leap seconds in 1972
    console.log(leapSeconds(d1, d2) == 2)

    const d3 = new Date('2000-01-01T00:00:00Z')
    const d4 = endOfYear(new Date('2005-01-01T00:00:00Z'), tzUtc)
    // There were no leap seconds in between 2000 and 2005
    console.log(leapSeconds(d3, d4) == 0)

    Parameters

    • firstDate: Date

      The first date.

    • secondDate: Date

      The second date.

    Returns number

    The number of leap seconds

  • roundDate(date: Date, tz?: Timezone): Date
  • Round a date to the start of the current day if before noon; otherwise the start of the next day.

    import { roundDate, tzLocal } from '@jetblack/date'

    const morning = tzLocal.makeDate(2000, 0, 1, 10, 30)
    console.log(tzLocal.toISOString(roundDate(morning, tzLocal)))
    // returns "2000-01-01T00:00:00+??:??"

    const afternoon = tzLocal.makeDate(2000, 0, 1, 18, 30)
    console.log(tzLocal.toISOString(roundDate(afternoon, tzLocal)))
    // returns "2000-01-02T00:00:00+??:??"

    const noon = tzLocal.makeDate(2000, 0, 1, 12, 0)
    console.log(tzLocal.toISOString(roundDate(noon, tzLocal)))
    // returns "2000-01-02T00:00:00+??:??"

    Parameters

    • date: Date

      The date to round

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the day of the rounded date.

Ranges Functions

  • dateRangeByDay(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
  • Creates a range of dates by day.

    import { dateRangeByDay, tzUtc } from '@jetblack/date'

    const dates = dateRangeByDay(
    new Date('2000-01-01T12:00:00Z'), // Sets to start of day.
    new Date('2000-01-10T12:00:00Z'),
    1,
    tzUtc
    )
    dates.forEach(date => console.log(date.toISOString()))
    // 2000-01-01T00:00:00.000Z
    // 2000-01-02T00:00:00.000Z
    // 2000-01-03T00:00:00.000Z
    // 2000-01-04T00:00:00.000Z
    // 2000-01-05T00:00:00.000Z
    // 2000-01-06T00:00:00.000Z
    // 2000-01-07T00:00:00.000Z
    // 2000-01-08T00:00:00.000Z
    // 2000-01-09T00:00:00.000Z
    // 2000-01-10T00:00:00.000Z

    Parameters

    • startDate: Date

      The start date.

    • endDate: Date

      The end date.

    • step: number = 1

      The day step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The range of dates separated by step days from the start to the end date.

  • dateRangeByHour(startDate: Date, endDate: Date, step?: number): Date[]
  • Creates a range of dates by hours.

    Parameters

    • startDate: Date

      The start date.

    • endDate: Date

      The end date.

    • step: number = 1

      The hour step count.

    Returns Date[]

    The range of dates separated by step hours from the start to the end hour.

  • dateRangeByMinute(startDate: Date, endDate: Date, step?: number): Date[]
  • Creates a range of dates by minutes.

    Parameters

    • startDate: Date

      The start date.

    • endDate: Date

      The end date.

    • step: number = 1

      The minute step count.

    Returns Date[]

    The range of dates separated by step hours from the start to the end minute.

  • dateRangeByMonth(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
  • Creates a range of dates by month.

    Parameters

    • startDate: Date

      The start date.

    • endDate: Date

      The end date.

    • step: number = 1

      The month step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The range of dates separated by step months from the start to the end date.

  • dateRangeByQuarter(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
  • Creates a range of dates by quarters.

    Parameters

    • startDate: Date

      The start date.

    • endDate: Date

      The end date.

    • step: number = 1

      The quarter step count. Defaults to 1.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The range of dates separated by step months from the start to the end date.

  • dateRangeBySecond(startDate: Date, endDate: Date, step?: number): Date[]
  • Creates a range of dates by seconds.

    Parameters

    • startDate: Date

      The start date.

    • endDate: Date

      The end date.

    • step: number = 1

      The second step count.

    Returns Date[]

    The range of dates separated by step hours from the start to the end second.

  • dateRangeByWeek(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
  • Creates a range of dates by week.

    Parameters

    • startDate: Date

      The start date.

    • endDate: Date

      The end date.

    • step: number = 1

      The week step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The range of dates separated by step weeks from the start to the end date.

  • dateRangeByYear(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
  • Creates a range of dates by year.

    Parameters

    • startDate: Date

      The start date.

    • endDate: Date

      The end date.

    • step: number = 1

      The year step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The range of dates separated by step years from the start to the end date.

  • dateSchedule(periodicity: Periodicity, date: Date, start: number, stop: number, step?: number, tz?: Timezone): Date[]
  • Creates a schedule of dates for a periodicity.

    Parameters

    • periodicity: Periodicity

      The periodicity.

    • date: Date

      The start date.

    • start: number

      Start offset periods.

    • stop: number

      The number of periods.

    • step: number = 1

      The period step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The schedule of dates.

  • dateScheduleByDay(date: Date, start: number, stop: number, step?: number, tz?: Timezone): Date[]
  • Creates a schedule of dates by day.

    import { dateScheduleByDay, tzLocal } from '@jetblack/date'

    console.log(
    dateScheduleByDay(tzLocal.makeDate(2000, 0, 1), 0, 7).map(x =>
    tzLocal.toISOString(x)
    )
    )
    // returns (7) ['2000-01-01T00:00:00+??:??', '2000-01-02T00:00:00+??:??', '2000-01-03T00:00:00+??:??', '2000-01-04T00:00:00+??:??', '2000-01-05T00:00:00+??:??', '2000-01-06T00:00:00+??:??', '2000-01-07T00:00:00+??:??']

    Parameters

    • date: Date

      The start date.

    • start: number

      Start offset days.

    • stop: number

      The number of days.

    • step: number = 1

      The days step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The schedule of dates separated by step days.

  • dateScheduleByMonth(date: Date, start: number, stop: number, step?: number, tz?: Timezone): Date[]
  • Creates a schedule of dates by month.

    import { dateScheduleByMonth, tzLocal } from '@jetblack/date'

    console.log(
    dateScheduleByMonth(tzLocal.makeDate(2000, 0, 1), 0, 6).map(x =>
    tzLocal.toISOString(x)
    )
    )
    // returns (6) ['2000-01-01T00:00:00+??:??', '2000-02-01T00:00:00+??:??', '2000-03-01T00:00:00+??:??', '2000-04-01T00:00:00+??:??', '2000-05-01T00:00:00+??:??', '2000-06-01T00:00:00+??:??']

    Parameters

    • date: Date

      The start date.

    • start: number

      Start offset months.

    • stop: number

      The number of months.

    • step: number = 1

      The months step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The schedule of dates separated by step months.

  • dateScheduleByQuarter(date: Date, start: number, stop: number, step?: number, tz?: Timezone): Date[]
  • Creates a schedule of dates by quarter.

    import { dateScheduleByQuarter, tzLocal } from '@jetblack/date'

    console.log(
    dateScheduleByQuarter(tzLocal.makeDate(2000, 0, 1), 0, 4).map(x =>
    tzLocal.toISOString(x)
    )
    )
    // returns (4) ['2000-01-01T00:00:00+??:??', '2000-04-01T00:00:00+??:??', '2000-07-01T00:00:00+??:??', '2000-10-01T00:00:00+??:??']

    Parameters

    • date: Date

      The start date.

    • start: number

      Start offset quarters.

    • stop: number

      The number of quarters.

    • step: number = 1

      The quarters step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The schedule of dates separated by step quarter.

  • dateScheduleByWeek(date: Date, start: number, stop: number, step?: number, tz?: Timezone): Date[]
  • Creates a schedule of dates by week.

    import { dateScheduleByWeek, tzLocal } from '@jetblack/date'

    console.log(
    dateScheduleByWeek(tzLocal.makeDate(2000, 0, 1), 0, 4).map(x =>
    tzLocal.toISOString(x)
    )
    )
    // returns (4) ['2000-01-01T00:00:00+??:??', '2000-01-08T00:00:00+??:??', '2000-01-15T00:00:00+??:??', '2000-01-22T00:00:00+??:??']

    Parameters

    • date: Date

      The start date.

    • start: number

      Start offset weeks.

    • stop: number

      The number of weeks.

    • step: number = 1

      The weeks step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The schedule of dates separated by step weeks.

  • dateScheduleByYear(date: Date, start: number, stop: number, step?: number, tz?: Timezone): Date[]
  • Creates a schedule of dates by year.

    import { dateScheduleByYear, tzLocal } from '@jetblack/date'

    console.log(
    dateScheduleByYear(tzLocal.makeDate(2000, 0, 1), 0, 4).map(x =>
    tzLocal.toISOString(x)
    )
    )
    // returns (4) ['2000-01-01T00:00:00+??:??', '2001-01-01T00:00:00+??:??', '2002-01-01T00:00:00+??:??', '2003-01-01T00:00:00+??:??']

    Parameters

    • date: Date

      The start date.

    • start: number

      Start offset years.

    • stop: number

      The number of years.

    • step: number = 1

      The years step count.

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The schedule of dates separated by step years.

Timezone Functions

  • Transform the values of a timezone offset.

    Parameters

    • data: object

      The object to transform.

    Returns TimezoneOffset

    The transformed object.

  • fetchTimezone(name: string, version?: string, rootUrl?: string, options?: RequestInit): Promise<IANATimezone>
  • Fetch a timezone.

    import { fetchTimezone } from '@jetblack/date'

    const tzChicago = await fetchTimezone('America/Chicago')
    console.log(tzChicago.makeDate(2022, 12, 25).toISOString())
    // 2023-01-25T06:00:00.000Z

    Parameters

    • name: string

      The timezone name.

    • version: string = 'latest'

      The database version.

    • rootUrl: string = 'https://cdn.jsdelivr.net/npm/@jetblack/tzdata/dist'

      The root url.

    • Optional options: RequestInit

      Fetch options

    Returns Promise<IANATimezone>

    A promise resolving to the timezone.

  • fetchTimezoneNames(version?: string, rootUrl?: string, options?: RequestInit): Promise<string[]>
  • Fetch a list of timezone names.

    import { fetchTimezoneNames } from '@jetblack/map'

    const names = await fetchTimezoneNames()
    console.log(names.filter(name => name.startsWith('Arctic')))
    // (1) ['Arctic/Longyearbyen']

    Parameters

    • version: string = 'latest'

      The database version.

    • rootUrl: string = 'https://cdn.jsdelivr.net/npm/@jetblack/tzdata/dist'

      The root url.

    • Optional options: RequestInit

      Fetch options

    Returns Promise<string[]>

    A promise resolving to the list of time zone names.

  • minDataToTimezoneOffset(data: { a: string; d: number; o: number; u: number }): TimezoneOffset
  • Transform the values of a minified timezone offset.

    Parameters

    • data: { a: string; d: number; o: number; u: number }

      The object to transform.

      • a: string
      • d: number
      • o: number
      • u: number

    Returns TimezoneOffset

    The transformed object.

  • timezoneFromJSON(name: string, tzdata: object[]): IANATimezone
  • Create a timezone from JSON data.

    The JSON data requires transformation to convert date and duration strings to objects.

    Parameters

    • name: string

      The timezone name.

    • tzdata: object[]

      The JSON timezone data.

    Returns IANATimezone

    The new timezone.

  • tzDataReviver(key: string, value: any): any
  • A JSON.parse reviver for tzdata.

    Parameters

    • key: string

      The JSON key.

    • value: any

      The JSON value.

    Returns any

    A revived value if applicable.

Generated using TypeDoc