From b87cd3301100d4ad740f6a9871c68631f91fd6a5 Mon Sep 17 00:00:00 2001 From: Rob Blackbourn Date: Sat, 15 Jul 2023 10:30:30 +0100 Subject: [PATCH] More docs --- docs/classes/Calendar.html | 6 +- docs/classes/Duration.html | 36 ++-- docs/classes/HolidayCalendar.html | 8 +- docs/classes/IANATimezone.html | 32 ++-- docs/classes/LocaleInfo.html | 10 +- docs/classes/Timezone.html | 32 ++-- docs/classes/WeekendCalendar.html | 8 +- docs/enums/BusinessDayConvention.html | 12 +- docs/interfaces/DatePartResponse.html | 2 +- docs/interfaces/DayPlurals.html | 2 +- docs/interfaces/I18nSettings.html | 10 +- docs/interfaces/TimezoneOffset.html | 8 +- docs/modules.html | 232 +++++++++++++------------- docs/pages/guide/date-arithmetic.html | 15 +- manual/guide/date-arithmetic.md | 66 ++++---- 15 files changed, 242 insertions(+), 237 deletions(-) diff --git a/docs/classes/Calendar.html b/docs/classes/Calendar.html index 3efe8d8..7522418 100644 --- a/docs/classes/Calendar.html +++ b/docs/classes/Calendar.html @@ -10,13 +10,13 @@

This is how the WeekendCalendar is defined.

import { Calendar, Timezone } from '@jetblack/date'

export class WeekendCalendar extends Calendar {
#weekends: number[]

constructor(name: string = 'WeekendCalendar', weekends: number[] = [0, 6]) {
super(name)
this.#weekends = weekends
}

isWeekend(date: Date, tz: Timezone): boolean {
const dayOfWeek = tz.weekday(date)
return this.#weekends.some(x => x === dayOfWeek)
}

isHoliday(date: Date, tz: Timezone): boolean {
return this.isWeekend(date)
}
}
-

Hierarchy

Index

Constructors

Accessors

Methods

Constructors

Hierarchy

Index

Constructors

Accessors

Methods

Constructors

Accessors

  • get name(): string

Returns Calendar

Accessors

  • get name(): string

Methods

  • isHoliday(date: Date, tz: Timezone): boolean

Methods

  • isHoliday(date: Date, tz: Timezone): boolean
  • Check if the date is a holiday.

    Parameters

    • date: Date

      The date to check.

      diff --git a/docs/classes/Duration.html b/docs/classes/Duration.html index d2cbfcd..e7d3795 100644 --- a/docs/classes/Duration.html +++ b/docs/classes/Duration.html @@ -6,7 +6,7 @@

      Note that durations may be negative.

      import { Duration } from '@jetblack/date'

      const duration = new Duration('-P1D')
      const d1 = addDuration(new Date('2000-01-01T00:00:00Z'), duration)
      console.log(d1)
      // Fri Dec 31 1999 00:00:00 GMT+0000 (Greenwich Mean Time)
      -

Hierarchy

  • Duration

Index

Constructors

  • new Duration(years: number, months: number, weeks?: number, days?: number, hours?: number, minutes?: number, seconds?: number): Duration
  • new Duration(value: string | number | Duration): Duration

Hierarchy

  • Duration

Index

Constructors

  • new Duration(years: number, months: number, weeks?: number, days?: number, hours?: number, minutes?: number, seconds?: number): Duration
  • new Duration(value: string | number | Duration): Duration
  • Constructs a duration.

    The resulting duration will be simplified. For example 12 months will become 1 year and 0 months.

    @@ -33,7 +33,7 @@

    The minutes.

  • Optional seconds: number

    The seconds.

    -

Returns Duration

  • +
  • Returns Duration

  • Constructs a year from the string representation, the number of seconds, or another duration object.

    The resulting duration will be simplified. For example 12 months will @@ -49,43 +49,43 @@

    Parameters

    • value: string | number | Duration

      The duration as a string, number, or duration.

      -

    Returns Duration

  • Accessors

    • get days(): number
    • set days(value: number): void

    Returns Duration

    Accessors

    • get days(): number
    • set days(value: number): void
    • Gets the days.

      -

      Returns number

    • +

      Returns number

    • Sets the days.

      If the value is greater than or equal to 7 the duration is simplified.

      -

      Parameters

      • value: number

      Returns void

    • get hours(): number
    • set hours(value: number): void
    • get hours(): number
    • set hours(value: number): void
    • Gets the hours.

      -

      Returns number

    • +

      Returns number

    • Sets the hours.

      If the value is greater than or equal to 24 the duration is simplified.

      -

      Parameters

      • value: number

      Returns void

    • get minutes(): number
    • set minutes(value: number): void
    • get minutes(): number
    • set minutes(value: number): void
    • Gets the minutes.

      -

      Returns number

    • +

      Returns number

    • Sets the minutes.

      If the value is greater than or equal to 60 the duration is simplified.

      -

      Parameters

      • value: number

      Returns void

    • get months(): number
    • set months(value: number): void
    • get months(): number
    • set months(value: number): void
    • Gets the months.

      -

      Returns number

    • +

      Returns number

    • Sets the months.

      If the value is greater than or equal to 12 the duration is simplified.

      -

      Parameters

      • value: number

      Returns void

    • get seconds(): number
    • set seconds(value: number): void
    • get seconds(): number
    • set seconds(value: number): void
    • Gets the seconds.

      -

      Returns number

    • +

      Returns number

    • Sets the seconds.

      If the value is greater than or equal to 60 the value is simplified.

      -

      Parameters

      • value: number

      Returns void

    • get weeks(): number
    • set weeks(value: number): void
    • get weeks(): number
    • set weeks(value: number): void
    • get years(): number
    • set years(value: number): void
    • get years(): number
    • set years(value: number): void

    Methods

    • [toPrimitive](hint: string): any
    • toString(): string

    Methods

    • [toPrimitive](hint: string): any
    • toString(): string
    • Creates a string representation of the duration.

      Returns string

      An ISO 8601 duration.

      -
    • valueOf(): number
    • valueOf(): number
    • The number of milliseconds of the duration assuming a year has 360 days and a month has 30 days.

      Note that this is not idempotent. As the actual number of days in a year diff --git a/docs/classes/HolidayCalendar.html b/docs/classes/HolidayCalendar.html index 535f844..b94179e 100644 --- a/docs/classes/HolidayCalendar.html +++ b/docs/classes/HolidayCalendar.html @@ -3,7 +3,7 @@

      The following constructs a holiday calendar for the US in 2020.

      import { HolidayCalendar } from '@jetblack/date'

      const calUS = new HolidayCalendar(
      'US',
      [0, 6], // Saturday and Sunday are always holidays.
      [
      new Date('2020-01-01'), // Wed: New Year's Day
      new Date('2020-01-20'), // Mon: Martin Luther King Jr. Day
      new Date('2020-05-25'), // Mon: Memorial Day
      new Date('2020-07-03'), // Fri: Independence Day
      new Date('2020-09-07'), // Mon: Labor Day
      new Date('2020-11-11'), // Wed: Veterans Day
      new Date('2020-11-26'), // Thu: Thanksgiving
      new Date('2020-12-25') // Fri: Christmas Day
      ]
      )
      -

    Hierarchy

    Index

    Constructors

    Accessors

    Methods

    Constructors

    Hierarchy

    Index

    Constructors

    Accessors

    Methods

    Constructors

    Accessors

    • get name(): string

    Returns HolidayCalendar

    Accessors

    • get name(): string
    • Get the calendar name.

      -

      Returns string

    Methods

    • isHoliday(date: Date, tz?: Timezone): boolean

    Methods

    • isHoliday(date: Date, tz?: Timezone): boolean
    • isWeekend(date: Date, tz?: Timezone): boolean
    • isWeekend(date: Date, tz?: Timezone): boolean
    • Check if the date is a weekend.

      Parameters

      • date: Date

        The date.

        diff --git a/docs/classes/IANATimezone.html b/docs/classes/IANATimezone.html index f576970..f34d612 100644 --- a/docs/classes/IANATimezone.html +++ b/docs/classes/IANATimezone.html @@ -3,15 +3,15 @@

        This example gets the data from the internet.

        import { IANATimezone, minDataToTimezoneOffset } from '@jetblack/date'

        const timezoneName = 'Europe/Brussels'
        fetch(`https://cdn.jsdelivr.net/npm/@jetblack/tzdata/dist/latest/${timezoneName}.min.json`)
        .then(response => response.json())
        .then(data => {
        const zoneData = data.map(minDataToTimezoneOffset)
        const tz = new IANATimezone(timeZoneName, zoneData)
        const newYearsDay = tz.makeDate(2000, 0, 1)
        // returns "2000-01-01T01:00:00Z"
        })
        .catch(error => console.error(error))
        }
        -

    Hierarchy

    Index

    Constructors

    Hierarchy

    Index

    Constructors

    Accessors

    • get name(): string

    Returns IANATimezone

    Accessors

    • get name(): string
    • Get the name of the timezone.

      -

      Returns string

    Methods

    Methods

    • Makes a new date by taking the date parts using this timezone and making the date with the supplied time zone.

      Parameters

      • date: Date
        @@ -19,52 +19,52 @@
      • tz: Timezone

        A timezone

      Returns Date

      A new date using the date parts from this timezone, constructed with the supplied timezone.

      -
    • Extract the date parts.

      If more than one date part is required this is the preferred way to get them, as the timezone calculation is only performed once.

      const {
      year,
      monthIndex,
      day,
      weekday,
      hours,
      minutes,
      seconds,
      milliseconds
      } = tz.dateParts(date)
      -

      Parameters

      • date: Date

      Returns DatePartResponse

    • day(date: Date): number
    • day(date: Date): number
    • The day of the month for the given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1)
      console.log(tzUtc.day(date))
      // returns 1
      -

      Parameters

      • date: Date

      Returns number

    • hours(date: Date): number
    • hours(date: Date): number
    • The hour of the day for the given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
      console.log(tzUtc.hours(date))
      // returns 12
      -

      Parameters

      • date: Date

      Returns number

    • isDaylightSavings(date: Date): boolean
    • isDaylightSavings(date: Date): boolean
    • makeDate(year: number, monthIndex: number, day?: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number): Date
    • makeDate(year: number, monthIndex: number, day?: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number): Date
    • Create a date from its component parts.

      -

      Parameters

      • year: number
      • monthIndex: number
      • Optional day: number
      • Optional hours: number
      • Optional minutes: number
      • Optional seconds: number
      • Optional milliseconds: number

      Returns Date

    • milliseconds(date: Date): number
    • +

      Parameters

      • year: number
      • monthIndex: number
      • Optional day: number
      • Optional hours: number
      • Optional minutes: number
      • Optional seconds: number
      • Optional milliseconds: number

      Returns Date

    • milliseconds(date: Date): number
    • The millisecond of the day for a given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
      console.log(tzUtc.milliseconds(date))
      // returns 123
      -

      Parameters

      • date: Date

      Returns number

    • minutes(date: Date): number
    • minutes(date: Date): number
    • The minute of the day for the given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
      console.log(tzUtc.minutes(date))
      // returns 15
      -

      Parameters

      • date: Date

      Returns number

    • monthIndex(date: Date): number
    • monthIndex(date: Date): number
    • The month index for the given date where 0 is January.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1)
      console.log(tzUtc.monthIndex(date))
      // returns 0
      -

      Parameters

      • date: Date

      Returns number

    • offset(date: Date): number
    • offset(date: Date): number
    • seconds(date: Date): number
    • seconds(date: Date): number
    • The second of the day for a given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
      console.log(tzUtc.seconds(date))
      // returns 30
      -

      Parameters

      • date: Date

      Returns number

    • toISOString(date: Date): string
    • toISOString(date: Date): string
    • The ISO 8601 date string representation for a given date.

      Parameters

      • date: Date

        The date.

      Returns string

      The ISO date string.

      -
    • weekday(date: Date): number
    • weekday(date: Date): number
    • The day of the week for the given date where 0 is Sunday.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1)
      console.log(tzUtc.weekday(date))
      // returns 6
      -

      Parameters

      • date: Date

      Returns number

    • year(date: Date): number
    • year(date: Date): number

    Hierarchy

    Index

    Constructors

    Hierarchy

    Index

    Constructors

    • Construct a new timezone.

      Parameters

      • name: string

        The timezone name.

        -

      Returns Timezone

    Accessors

    • get name(): string

    Returns Timezone

    Accessors

    • get name(): string
    • Get the name of the timezone.

      -

      Returns string

    Methods

    Methods

    • Makes a new date by taking the date parts using this timezone and making the date with the supplied time zone.

      Parameters

      • date: Date
        @@ -33,7 +33,7 @@
      • tz: Timezone

        A timezone

      Returns Date

      A new date using the date parts from this timezone, constructed with the supplied timezone.

      -
    • Extract the date parts.

      If more than one date part is required this is the preferred way to get them, as the timezone calculation is only performed once.

      @@ -42,26 +42,26 @@

      Parameters

      • date: Date

        The date.

      Returns DatePartResponse

      The date parts.

      -
    • day(date: Date): number
    • day(date: Date): number
    • The day of the month for the given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1)
      console.log(tzUtc.day(date))
      // returns 1

      Parameters

      • date: Date

        The date.

      Returns number

      The day of the month.

      -
    • hours(date: Date): number
    • hours(date: Date): number
    • The hour of the day for the given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
      console.log(tzUtc.hours(date))
      // returns 12

      Parameters

      • date: Date

        The date.

      Returns number

      The hour of the day.

      -
    • isDaylightSavings(date: Date): boolean
    • isDaylightSavings(date: Date): boolean
    • Find if the date was subject to daylight savings time.

      Parameters

      • date: Date

        The date.

      Returns boolean

      True if the date was subject to daylight savings time.

      -
    • makeDate(year: number, monthIndex: number, day?: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number): Date
    • makeDate(year: number, monthIndex: number, day?: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number): Date
    • Create a date from its component parts.

      Parameters

      • year: number

        The year.

        @@ -78,51 +78,51 @@
      • Optional milliseconds: number

        The millisecond of the day.

      Returns Date

      A new date built from the parts.

      -
    • milliseconds(date: Date): number
    • milliseconds(date: Date): number
    • The millisecond of the day for a given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
      console.log(tzUtc.milliseconds(date))
      // returns 123

      Parameters

      • date: Date

        The date.

      Returns number

      The milliseconds of the day.

      -
    • minutes(date: Date): number
    • minutes(date: Date): number
    • The minute of the day for the given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
      console.log(tzUtc.minutes(date))
      // returns 15

      Parameters

      • date: Date

        The date.

      Returns number

      The minute of the day.

      -
    • monthIndex(date: Date): number
    • monthIndex(date: Date): number
    • The month index for the given date where 0 is January.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1)
      console.log(tzUtc.monthIndex(date))
      // returns 0

      Parameters

      • date: Date

        The date.

      Returns number

      The month index of the date where 0 is January.

      -
    • offset(date: Date): number
    • offset(date: Date): number
    • The signed offset in minutes from UTC for the given date.

      Parameters

      • date: Date

        The date.

        -

      Returns number

    • seconds(date: Date): number

    Returns number

    • seconds(date: Date): number
    • The second of the day for a given date.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
      console.log(tzUtc.seconds(date))
      // returns 30

      Parameters

      • date: Date

        The date.

      Returns number

      The second of the day.

      -
    • toISOString(date: Date): string
    • toISOString(date: Date): string
    • The ISO 8601 date string representation for a given date.

      Parameters

      • date: Date

        The date.

      Returns string

      The ISO date string.

      -
    • weekday(date: Date): number
    • weekday(date: Date): number
    • The day of the week for the given date where 0 is Sunday.

      import { tzUtc } from '@jetblack/date'

      const date = tzUtc.makeDate(2000, 0, 1)
      console.log(tzUtc.weekday(date))
      // returns 6

      Parameters

      • date: Date

        The date.

      Returns number

      The day of the week where 0 is Sunday.

      -
    • year(date: Date): number
    • year(date: Date): number

    Accessors

    • get name(): string
    • Get the calendar name.

      -

      Returns string

    Methods

    • isHoliday(date: Date, tz?: Timezone): boolean

    Methods

    • isHoliday(date: Date, tz?: Timezone): boolean
    • Check if the date is a holiday.

      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 a holiday, otherwise false.

      -
    • isWeekend(date: Date, tz?: Timezone): boolean
    • isWeekend(date: Date, tz?: Timezone): boolean
    • Check if the date is a weekend.

      Parameters

      • date: Date

        The date.

        diff --git a/docs/enums/BusinessDayConvention.html b/docs/enums/BusinessDayConvention.html index 030cc04..fade845 100644 --- a/docs/enums/BusinessDayConvention.html +++ b/docs/enums/BusinessDayConvention.html @@ -5,16 +5,16 @@ nearest non-holiday.

        The modified rules determine what to do when the adjustment is at the end of the month.

        -

    Index

    Enumeration members

    FOLLOWING = 3
    +

    Index

    Enumeration members

    FOLLOWING = 3

    Adjust to the nearest business day after the current day

    -
    MODIFIED_FOLLOWING = 5
    +
    MODIFIED_FOLLOWING = 5

    Adjust to the nearest business day after the current day withing the month; otherwise adjust following

    -
    MODIFIED_PRECEDING = 4
    +
    MODIFIED_PRECEDING = 4

    Adjust to the nearest business day after the current day withing the month; otherwise adjust preceding

    -
    NEAREST = 1
    +
    NEAREST = 1

    Adjust to the nearest business day

    -
    NONE = 0
    +
    NONE = 0

    No adjustment

    -
    PRECEDING = 2
    +
    PRECEDING = 2

    Adjust to the nearest business day before the current day

    Legend

    • Constructor
    • Property
    • Method
    • Accessor
    • Property
    • Inherited method

    Settings

    Theme

    Generated using TypeDoc

    \ No newline at end of file diff --git a/docs/interfaces/DatePartResponse.html b/docs/interfaces/DatePartResponse.html index 988c1e6..4574ee0 100644 --- a/docs/interfaces/DatePartResponse.html +++ b/docs/interfaces/DatePartResponse.html @@ -1,3 +1,3 @@ DatePartResponse | @jetblack/date
    Options
    All
    • Public
    • Public/Protected
    • All
    Menu

    Interface DatePartResponse

    A response for date parts.

    -

    Hierarchy

    • DatePartResponse

    Index

    Properties

    day: number
    hours: number
    milliseconds: number
    minutes: number
    monthIndex: number
    seconds: number
    weekday: number
    year: number

    Legend

    • Constructor
    • Property
    • Method
    • Accessor
    • Property
    • Inherited method

    Settings

    Theme

    Generated using TypeDoc

    \ No newline at end of file +

    Hierarchy

    • DatePartResponse

    Index

    Properties

    day: number
    hours: number
    milliseconds: number
    minutes: number
    monthIndex: number
    seconds: number
    weekday: number
    year: number

    Legend

    • Constructor
    • Property
    • Method
    • Accessor
    • Property
    • Inherited method

    Settings

    Theme

    Generated using TypeDoc

    \ No newline at end of file diff --git a/docs/interfaces/DayPlurals.html b/docs/interfaces/DayPlurals.html index a8c8b4a..c57940c 100644 --- a/docs/interfaces/DayPlurals.html +++ b/docs/interfaces/DayPlurals.html @@ -1,4 +1,4 @@ DayPlurals | @jetblack/date
    Options
    All
    • Public
    • Public/Protected
    • All
    Menu

    Interface DayPlurals

    Day plurals.

    The names from Intl.PluralRules.

    -

    Hierarchy

    • DayPlurals

    Index

    Properties

    few: string
    many: string
    one: string
    other: string
    two: string
    zero: string

    Legend

    • Constructor
    • Property
    • Method
    • Accessor
    • Property
    • Inherited method

    Settings

    Theme

    Generated using TypeDoc

    \ No newline at end of file +

    Hierarchy

    • DayPlurals

    Index

    Properties

    few: string
    many: string
    one: string
    other: string
    two: string
    zero: string

    Legend

    • Constructor
    • Property
    • Method
    • Accessor
    • Property
    • Inherited method

    Settings

    Theme

    Generated using TypeDoc

    \ No newline at end of file diff --git a/docs/interfaces/I18nSettings.html b/docs/interfaces/I18nSettings.html index 2d255d8..84a7b7e 100644 --- a/docs/interfaces/I18nSettings.html +++ b/docs/interfaces/I18nSettings.html @@ -1,13 +1,13 @@ I18nSettings | @jetblack/date
    Options
    All
    • Public
    • Public/Protected
    • All
    Menu

    Interface I18nSettings

    The settings for internationalization.

    -

    Hierarchy

    • I18nSettings

    Implemented by

    Index

    Properties

    dayPeriod: Record<NameStyle, DayPeriods>
    +

    Hierarchy

    • I18nSettings

    Implemented by

    Index

    Properties

    dayPeriod: Record<NameStyle, DayPeriods>

    The day period names

    -
    dayPlurals: string[]
    +
    dayPlurals: string[]

    The plurals of the days of the month for 31 days starting at 0

    -
    locale: string
    +
    locale: string

    The locale name

    -
    month: Record<NameStyle, Months>
    +
    month: Record<NameStyle, Months>

    The names of the months

    -
    weekday: Record<NameStyle, Days>
    +
    weekday: Record<NameStyle, Days>

    The names of the days of the week

    Legend

    • Constructor
    • Property
    • Method
    • Accessor
    • Property
    • Inherited method

    Settings

    Theme

    Generated using TypeDoc

    \ No newline at end of file diff --git a/docs/interfaces/TimezoneOffset.html b/docs/interfaces/TimezoneOffset.html index 5f3d85a..fcb0594 100644 --- a/docs/interfaces/TimezoneOffset.html +++ b/docs/interfaces/TimezoneOffset.html @@ -1,11 +1,11 @@ TimezoneOffset | @jetblack/date
    Options
    All
    • Public
    • Public/Protected
    • All
    Menu

    Interface TimezoneOffset

    A line from the tzdata database.

    -

    Hierarchy

    • TimezoneOffset

    Index

    Properties

    abbr: string
    +

    Hierarchy

    • TimezoneOffset

    Index

    Properties

    abbr: string

    The common timezone abbreviation.

    -
    isDst: boolean
    +
    isDst: boolean

    If true the adjustment includes daylight savings time.

    -
    offset: number
    +
    offset: number

    The offset to be added to local time in minutes.

    -
    utc: number
    +
    utc: number

    The from which this offset applies in milliseconds since 1 Jan 1970 UTC.

    Legend

    • Constructor
    • Property
    • Method
    • Accessor
    • Property
    • Inherited method

    Settings

    Theme

    Generated using TypeDoc

    \ No newline at end of file diff --git a/docs/modules.html b/docs/modules.html index fc3047c..6fdd151 100644 --- a/docs/modules.html +++ b/docs/modules.html @@ -20,21 +20,21 @@ // 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]

    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]
    +
    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]
    +
    Months: [string, string, string, string, string, string, string, string, string, string, string, string]

    The 12 months of the year

    -
    NameStyle: "narrow" | "short" | "long"
    +
    NameStyle: "narrow" | "short" | "long"

    The Intl name style.

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

    Calendars Variables

    calWeekends: WeekendCalendar = ...
    +
    Periodicity: "daily" | "weekly" | "monthly" | "quarterly" | "yearly"

    Calendars Variables

    calWeekends: WeekendCalendar = ...

    The default calendar where Saturday and Sunday are considered holidays.

    -

    Timezone Variables

    tzLocal: LocalTimezone = ...
    +

    Timezone Variables

    tzLocal: LocalTimezone = ...

    The local timezone.

    -
    tzUtc: UtcTimezone = ...
    +
    tzUtc: UtcTimezone = ...

    The timezone for UTC.

    -

    Anchors Functions

    • endOfDay(date: Date, tz?: Timezone): Date

    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
      @@ -43,21 +43,21 @@
    • 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
    • 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
    • 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
    • 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
      @@ -66,26 +66,26 @@
    • 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
    • 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
    • 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
    • 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 @@ -95,7 +95,7 @@
    • 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
    • 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
      @@ -106,7 +106,7 @@
    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The end of the week.

    -
    • endOfYear(date: Date, tz?: Timezone): Date
    • 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
      @@ -115,7 +115,7 @@
    • 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
    • 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
      @@ -124,7 +124,7 @@
    • 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
    • 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-??:??
      @@ -133,7 +133,7 @@
    • 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
    • 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 @@ -143,7 +143,7 @@
    • 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
    • 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
      @@ -154,7 +154,7 @@
    • 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
    • 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
      @@ -163,17 +163,17 @@
    • 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
    • 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
    • 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+??:??"
      @@ -182,12 +182,12 @@
    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date

    The start of the day.

    -
    • startOfHour(date: Date): Date
    • 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
    • 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
        @@ -195,50 +195,50 @@
      • tz: Timezone = tzLocal

        An optional timezone. Defaults to the local timezone.

      Returns Date

      The start of the week.

      -
    • startOfMinute(date: Date): Date
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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.

        @@ -247,14 +247,14 @@
      • tz: Timezone = tzLocal

        An optional timezone. Defaults to the local timezone.

      Returns Date

      The start of the week.

      -
    • startOfYear(date: Date, tz?: Timezone): Date
    • 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

    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()) @@ -268,7 +268,7 @@
    • 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
    • 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 @@ -286,7 +286,7 @@

    • 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)
      @@ -297,7 +297,7 @@
    • 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
    • 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) @@ -307,7 +307,7 @@
    • 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
    • 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
      @@ -316,7 +316,7 @@
    • 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
    • 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
      @@ -325,7 +325,7 @@
    • 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
    • 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.

      @@ -338,7 +338,7 @@
    • 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
    • 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 @@ -357,7 +357,7 @@

    • 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
    • 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.

      @@ -368,7 +368,7 @@
    • 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
    • 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
      @@ -377,7 +377,7 @@
    • 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
    • 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) @@ -389,7 +389,7 @@
    • 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
        @@ -399,7 +399,7 @@
      • 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

    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
      @@ -408,21 +408,21 @@
    • 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
    • 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
    • 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
    • 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
        @@ -430,7 +430,7 @@
      • 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
    • 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 @@ -440,14 +440,14 @@
    • 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
    • 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
    • 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
      @@ -456,7 +456,7 @@
    • 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

    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
      @@ -467,7 +467,7 @@
    • 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
    • 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)
      @@ -476,7 +476,7 @@
    • 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
    • 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)
      @@ -485,7 +485,7 @@
    • 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
    • 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
      @@ -494,7 +494,7 @@
    • 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
    • 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
      @@ -503,7 +503,7 @@
    • rhs: Date

      The right date.

    Returns boolean

    True if the dates are not equal, otherwise false.

    -
    • isDateOnOrAfter(lhs: Date, rhs: Date): boolean
    • 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
      @@ -512,7 +512,7 @@
    • 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
    • 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
      @@ -521,14 +521,14 @@
    • 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
    • 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
    • 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
      @@ -546,7 +546,7 @@ // returns true console.log(isDateBefore(new Date('2000-01-01'), new Date('2000-01-01'))) // returns false -``` Functions
      • isDateBefore(lhs: Date, rhs: Date): boolean
      • +``` Functions
        • isDateBefore(lhs: Date, rhs: Date): boolean
        • Checks if the left date is before the right date.

          Parameters

          • lhs: Date

            The left date.

            @@ -564,14 +564,14 @@ // returns false console.log(isDateEqual(new Date('2000-01-01'), new Date('2000-01-01'))) // returns true -``` Functions
            • isDateEqual(lhs: Date, rhs: Date): boolean
            • +``` 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

            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
              @@ -582,7 +582,7 @@
            • 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
        • diffInCalMonths(leftDate: Date, rightDate: Date, tz?: Timezone): number
        • Find the number of whole months between two dates.

          Parameters

          • leftDate: Date

            The left date.

            @@ -591,7 +591,7 @@
          • 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
        • diffInCalYears(leftDate: Date, rightDate: Date, tz?: Timezone): number
        • Find the number of whole years between two dates.

          Parameters

          • leftDate: Date

            The left date.

            @@ -600,7 +600,7 @@
          • 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
        • 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
          @@ -609,35 +609,35 @@
        • 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
      • 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
      • 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
      • 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
      • 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

    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
      @@ -778,7 +778,7 @@

      Pattern items

    • 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
    • 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.

      @@ -913,7 +913,7 @@

      Pattern items

    • 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

    Miscellaneous Functions

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

      Parameters

      • date: Date

        The start date.

        @@ -924,7 +924,7 @@

        Pattern items

      • tz: Timezone = tzLocal

        An optional timezone. Defaults to the local timezone.

      Returns Date

      The nearest business date.

      -
    • weekOfYear(date: Date, tz?: Timezone): number
    • 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 @@ -934,14 +934,14 @@

      Pattern items

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns number

    The week of the year.

    -
    • weekYear(date: Date, tz?: Timezone): number
    • 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

    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)
      @@ -956,8 +956,8 @@

      Pattern items

    • 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
    • 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
      @@ -966,7 +966,7 @@

      Pattern items

    • 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
    • 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

        @@ -975,14 +975,14 @@

        Pattern items

      • 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
    • 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
    • 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

        @@ -991,21 +991,21 @@

        Pattern items

      • 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
    • 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
    • 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
    • findLastWeekdayIndex(dates: Date[], weekday: number, tz?: Timezone): number
    • Find the index of the last date with the given weekday.

      Parameters

      • dates: Date[]

        The dates

        @@ -1014,14 +1014,14 @@

        Pattern items

      • 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
    • 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
    • findWeekdayIndex(dates: Date[], weekday: number, tz?: Timezone): number
    • Find the index of the first date with the given weekday.

      Parameters

      • dates: Date[]

        The dates

        @@ -1030,18 +1030,18 @@

        Pattern items

      • 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

    Returns I18nSettings

    • 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[]
    • 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]
      @@ -1050,14 +1050,14 @@

      Pattern items

    • 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
    • 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
    • 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)
      @@ -1066,7 +1066,7 @@

      Pattern items

    • secondDate: Date

      The second date.

    Returns number

    The number of leap seconds

    -
    • roundDate(date: Date, tz?: Timezone): Date
    • 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+??:??" @@ -1076,7 +1076,7 @@

      Pattern items

    • 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[]

    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
      @@ -1089,7 +1089,7 @@

      Pattern items

    • 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[]
    • dateRangeByHour(startDate: Date, endDate: Date, step?: number): Date[]
    • Creates a range of dates by hours.

      Parameters

      • startDate: Date

        The start date.

        @@ -1098,7 +1098,7 @@

        Pattern items

      • 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[]
    • dateRangeByMinute(startDate: Date, endDate: Date, step?: number): Date[]
    • Creates a range of dates by minutes.

      Parameters

      • startDate: Date

        The start date.

        @@ -1107,7 +1107,7 @@

        Pattern items

      • 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[]
    • dateRangeByMonth(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
    • Creates a range of dates by month.

      Parameters

      • startDate: Date

        The start date.

        @@ -1118,7 +1118,7 @@

        Pattern items

      • 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[]
    • dateRangeByQuarter(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
    • Creates a range of dates by quarters.

      Parameters

      • startDate: Date

        The start date.

        @@ -1129,7 +1129,7 @@

        Pattern items

      • 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[]
    • dateRangeBySecond(startDate: Date, endDate: Date, step?: number): Date[]
    • Creates a range of dates by seconds.

      Parameters

      • startDate: Date

        The start date.

        @@ -1138,7 +1138,7 @@

        Pattern items

      • 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[]
    • dateRangeByWeek(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
    • Creates a range of dates by week.

      Parameters

      • startDate: Date

        The start date.

        @@ -1149,7 +1149,7 @@

        Pattern items

      • 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[]
    • dateRangeByYear(startDate: Date, endDate: Date, step?: number, tz?: Timezone): Date[]
    • Creates a range of dates by year.

      Parameters

      • startDate: Date

        The start date.

        @@ -1160,7 +1160,7 @@

        Pattern items

      • 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[]
    • 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.

        @@ -1175,7 +1175,7 @@

        Pattern items

      • 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[]
    • 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+??:??']
      @@ -1190,7 +1190,7 @@

      Pattern items

    • 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[]
    • 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+??:??']
      @@ -1205,7 +1205,7 @@

      Pattern items

    • 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[]
    • 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+??:??']
      @@ -1220,7 +1220,7 @@

      Pattern items

    • 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[]
    • 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+??:??']
      @@ -1235,7 +1235,7 @@

      Pattern items

    • 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[]
    • 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+??:??']
      @@ -1250,12 +1250,12 @@

      Pattern items

    • tz: Timezone = tzLocal

      An optional timezone. Defaults to the local timezone.

    Returns Date[]

    The schedule of dates separated by step years.

    -

    Timezone Functions

    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>
    • 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
      @@ -1268,7 +1268,7 @@

      Pattern items

    • Optional options: RequestInit

      Fetch options

    Returns Promise<IANATimezone>

    A promise resolving to the timezone.

    -
    • fetchTimezoneNames(version?: string, rootUrl?: string, options?: RequestInit): Promise<string[]>
    • 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']
      @@ -1279,12 +1279,12 @@

      Pattern items

    • 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
    • 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
    • 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.

      @@ -1293,7 +1293,7 @@

      Pattern items

    • tzdata: object[]

      The JSON timezone data.

    Returns IANATimezone

    The new timezone.

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

      Parameters

      • key: string

        The JSON key.

        diff --git a/docs/pages/guide/date-arithmetic.html b/docs/pages/guide/date-arithmetic.html index 3b9a205..5da1259 100644 --- a/docs/pages/guide/date-arithmetic.html +++ b/docs/pages/guide/date-arithmetic.html @@ -10,16 +10,13 @@

        Start of Day

        Daylight Savings Time

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

        -

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

        -
        import { addDays, tzLocal, tzUtc }

        // 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')
        console.log(d1.toString())
        // Sat Mar 25 2000 12:00:00 GMT+0000 (Greenwich Mean Time)

        const d2 = addDays(d1, 2, tzLocal)
        console.log(d2.toString())
        // Mon Mar 27 2000 12:00:00 GMT+0100 (British Summer Time)
        console.log((d2.getTime() - d1.getTime()) / 1000 / 60 / 60 + ' hours')
        // 47 hours

        const d3 = addDays(d1, 2, tzUtc)
        console.log(d3.toString())
        // Mon Mar 27 2000 13:00:00 GMT+0100 (British Summer Time)
        console.log((d3.getTime() - d1.getTime()) / 1000 / 60 / 60 + ' hours')
        // 48 hours +

        The following example demonstrates how daylight savings time is handled.

        +
        import { tzUtc, fetchTimezone, addDays, addHours } from '@jetblack/date'

        // In London on Sunday March 26 2000 the clocks went forward 1 hour at 1am.

        // At midnight the clocks have yet to be put forward.
        const tzLondon = await fetchTimezone('Europe/London')
        const mar26 = tzLondon.makeDate(2000, 2, 26)
        console.log(tzLondon.toISOString(mar26))
        // 2000-03-26T00:00:00+00:00
        console.log(tzUtc.toISOString(mar26))
        // 2000-03-26T00:00:00.000Z

        // Adding one hour from midnight takes the time to 2am as the clocks go forward
        // one hour at 1am.
        const mar26plus1h = addHours(mar26, 1)
        console.log(tzLondon.toISOString(mar26plus1h))
        // 2000-03-26T02:00:00+01:00
        console.log(tzUtc.toISOString(mar26plus1h))
        // 2000-03-26T01:00:00.000Z

        // Add a day in the context of the London time zone.
        const mar27 = addDays(mar26, 1, tzLondon)
        console.log(tzLondon.toISOString(mar27))
        // 2000-03-27T00:00:00+01:00
        console.log(tzUtc.toISOString(mar27))
        // 2000-03-26T23:00:00.000Z

        // There were only 23 hours in the day of the 26th.
        const millisInHour = 1000 * 60 * 60
        const hours26to27 = (mar27.getTime() - mar26.getTime()) / millisInHour
        console.log(hours26to27)
        // 23
        -

        When using the local timezone the time of day appears unchanged, and the offset -become "GMT+0100 (British Summer Time)". When using UTC the time looks wrong! -Looking at the time differences we see that adding 2 days in the local timezone -was 47 hours, while with UTC it was 48 hours.

        -

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

        +

        The London timezone is the same as UTC in the winter, but an hour ahead in the +summer. On the midnight of the day of the change London and UTC show the same +time. Adding an hour shows how the time leaps to 2am in London, but only 1am +in UTC. Adding a day and calculating the number of hours shows 23.

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

        diff --git a/manual/guide/date-arithmetic.md b/manual/guide/date-arithmetic.md index 993f43c..9a21972 100644 --- a/manual/guide/date-arithmetic.md +++ b/manual/guide/date-arithmetic.md @@ -8,39 +8,47 @@ the timezone. Daylight savings time (DST) presents an issue for date arithmetic. -The following example adds two days to the Saturday before the clocks -change using the local timezone (London) and UTC (which does not have DST). +The following example demonstrates how daylight savings time is handled. ```js -import { addDays, tzLocal, tzUtc } - -// 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') -console.log(d1.toString()) -// Sat Mar 25 2000 12:00:00 GMT+0000 (Greenwich Mean Time) - -const d2 = addDays(d1, 2, tzLocal) -console.log(d2.toString()) -// Mon Mar 27 2000 12:00:00 GMT+0100 (British Summer Time) -console.log((d2.getTime() - d1.getTime()) / 1000 / 60 / 60 + ' hours') -// 47 hours - -const d3 = addDays(d1, 2, tzUtc) -console.log(d3.toString()) -// Mon Mar 27 2000 13:00:00 GMT+0100 (British Summer Time) -console.log((d3.getTime() - d1.getTime()) / 1000 / 60 / 60 + ' hours') -// 48 hours +import { tzUtc, fetchTimezone, addDays, addHours } from '@jetblack/date' + +// In London on Sunday March 26 2000 the clocks went forward 1 hour at 1am. + +// At midnight the clocks have yet to be put forward. +const tzLondon = await fetchTimezone('Europe/London') +const mar26 = tzLondon.makeDate(2000, 2, 26) +console.log(tzLondon.toISOString(mar26)) +// 2000-03-26T00:00:00+00:00 +console.log(tzUtc.toISOString(mar26)) +// 2000-03-26T00:00:00.000Z + +// Adding one hour from midnight takes the time to 2am as the clocks go forward +// one hour at 1am. +const mar26plus1h = addHours(mar26, 1) +console.log(tzLondon.toISOString(mar26plus1h)) +// 2000-03-26T02:00:00+01:00 +console.log(tzUtc.toISOString(mar26plus1h)) +// 2000-03-26T01:00:00.000Z + +// Add a day in the context of the London time zone. +const mar27 = addDays(mar26, 1, tzLondon) +console.log(tzLondon.toISOString(mar27)) +// 2000-03-27T00:00:00+01:00 +console.log(tzUtc.toISOString(mar27)) +// 2000-03-26T23:00:00.000Z + +// There were only 23 hours in the day of the 26th. +const millisInHour = 1000 * 60 * 60 +const hours26to27 = (mar27.getTime() - mar26.getTime()) / millisInHour +console.log(hours26to27) +// 23 ``` -When using the local timezone the time of day appears unchanged, and the offset -become "GMT+0100 (British Summer Time)". When using UTC the time looks wrong! -Looking at the time differences we see that adding 2 days in the local timezone -was 47 hours, while with UTC it was 48 hours. - -With the local timezone the "naturally correct" answer is returned. -However, the elapsed time will be an hour less than when using UTC. +The London timezone is the same as UTC in the winter, but an hour ahead in the +summer. On the midnight of the day of the change London and UTC show the same +time. Adding an hour shows how the time leaps to 2am in London, but only 1am +in UTC. Adding a day and calculating the number of hours shows 23. Keeping the time change constant (as with UTC) can be useful when plotting data, or doing time series calculations (rolling averages, resampling, etc.).