Options
All
  • Public
  • Public/Protected
  • All
Menu

The base class for timezones.

Two timezones are provided by default: tzLocal and tzUtc. There is also a class IANATimezone which can be used with the time zone database maintained by IANA. All timezone sensitive operations take a timezone as an optional parameter which defaults to tzLocal.

There is a tutorial here.

The timezone object provides accessors for the common properties of a date such as Timezone.year. Here is an example of using the year.

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

const utcMillennium = new Date("2000-01-01T00:00:00Z")

// The UTC year will always be 2000
console.log(tzUtc.year(utcMillennium) === 2000)

// The year in the local timezone will depend where you are.
// If the timezone was New York ...
console.log(tzLocal.year(utcMillennium) === 1999)

There are two complimentary methods used for date construction: makeDate and dateParts. These are used by the library functions to efficiently deconstruct and construct dates for performing calculations.

There are two functions specific to timezones: isDaylightSavings and toISOString. The JavaScript built in Date.toISOString is only aware of the UTC timezone. This method provides support for any defined timezone.

Hierarchy

  • Timezone

Index

Constructors

  • Construct a new timezone.

    Parameters

    • name: string

      The timezone name.

    Returns Timezone

Accessors

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

    Returns string

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

      A date

    • 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

      The date.

    Returns DatePartResponse

    The date parts.

  • 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
  • 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
  • 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
  • Create a date from its component parts.

    Parameters

    • year: number

      The year.

    • monthIndex: number

      The month index where January is 0.

    • Optional day: number

      The day of the month.

    • Optional hours: number

      The hour of the day.

    • Optional minutes: number

      The minute of the day.

    • Optional seconds: number

      The second of the day.

    • Optional milliseconds: number

      The millisecond of the day.

    Returns Date

    A new date built from the parts.

  • 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
  • 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
  • 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
  • The signed offset in minutes from UTC for the given date.

    Parameters

    • date: Date

      The date.

    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
  • 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
  • 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
  • The year for the date.

    import { tzUtc } from '@jetblack/date'

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

    Parameters

    • date: Date

      The date.

    Returns number

    The year.

Generated using TypeDoc