Class DateTime

A DateTime is an immutable data structure representing a specific date and time and accompanying methods. It contains class and instance methods for creating, parsing, interrogating, transforming, and formatting them.

A DateTime comprises of:

  • A timestamp. Each DateTime instance refers to a specific millisecond of the Unix epoch.
  • A time zone. Each instance is considered in the context of a specific zone (by default the system's time zone).
  • Configuration properties that effect how output strings are formatted, such as locale, numberingSystem, and outputCalendar.

Here is a brief overview of the most commonly used functionality it provides:

There's plenty others documented below. In addition, for more information on subtler topics like internationalization, time zones, alternative calendars, validity, and so on, see the external documentation.

Hierarchy

  • DateTime

Properties

DATETIME_FULL: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "long";
    timeZoneName: "short";
    year: "numeric";
} = Formats.DATETIME_FULL

toLocaleString format like 'October 14, 1983, 9:30 AM EDT'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "long"
  • timeZoneName: "short"
  • year: "numeric"
DATETIME_FULL_WITH_SECONDS: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "long";
    second: "numeric";
    timeZoneName: "short";
    year: "numeric";
} = Formats.DATETIME_FULL_WITH_SECONDS

toLocaleString format like 'October 14, 1983, 9:30:33 AM EDT'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "long"
  • second: "numeric"
  • timeZoneName: "short"
  • year: "numeric"
DATETIME_HUGE: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "long";
    timeZoneName: "long";
    weekday: "long";
    year: "numeric";
} = Formats.DATETIME_HUGE

toLocaleString format like 'Friday, October 14, 1983, 9:30 AM Eastern Daylight Time'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "long"
  • timeZoneName: "long"
  • weekday: "long"
  • year: "numeric"
DATETIME_HUGE_WITH_SECONDS: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "long";
    second: "numeric";
    timeZoneName: "long";
    weekday: "long";
    year: "numeric";
} = Formats.DATETIME_HUGE_WITH_SECONDS

toLocaleString format like 'Friday, October 14, 1983, 9:30:33 AM Eastern Daylight Time'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "long"
  • second: "numeric"
  • timeZoneName: "long"
  • weekday: "long"
  • year: "numeric"
DATETIME_MED: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "short";
    year: "numeric";
} = Formats.DATETIME_MED

toLocaleString format like 'Oct 14, 1983, 9:30 AM'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "short"
  • year: "numeric"
DATETIME_MED_WITH_SECONDS: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "short";
    second: "numeric";
    year: "numeric";
} = Formats.DATETIME_MED_WITH_SECONDS

toLocaleString format like 'Oct 14, 1983, 9:30:33 AM'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "short"
  • second: "numeric"
  • year: "numeric"
DATETIME_MED_WITH_WEEKDAY: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "short";
    weekday: "short";
    year: "numeric";
} = Formats.DATETIME_MED_WITH_WEEKDAY

toLocaleString format like 'Fri, 14 Oct 1983, 9:30 AM'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "short"
  • weekday: "short"
  • year: "numeric"
DATETIME_SHORT: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "numeric";
    year: "numeric";
} = Formats.DATETIME_SHORT

toLocaleString format like '10/14/1983, 9:30 AM'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "numeric"
  • year: "numeric"
DATETIME_SHORT_WITH_SECONDS: {
    day: "numeric";
    hour: "numeric";
    minute: "numeric";
    month: "numeric";
    second: "numeric";
    year: "numeric";
} = Formats.DATETIME_SHORT_WITH_SECONDS

toLocaleString format like '10/14/1983, 9:30:33 AM'. Only 12-hour if the locale is.

Type declaration

  • day: "numeric"
  • hour: "numeric"
  • minute: "numeric"
  • month: "numeric"
  • second: "numeric"
  • year: "numeric"
DATE_FULL: {
    day: "numeric";
    month: "long";
    year: "numeric";
} = Formats.DATE_FULL

toLocaleString format like 'October 14, 1983'

Type declaration

  • day: "numeric"
  • month: "long"
  • year: "numeric"
DATE_HUGE: {
    day: "numeric";
    month: "long";
    weekday: "long";
    year: "numeric";
} = Formats.DATE_HUGE

toLocaleString format like 'Tuesday, October 14, 1983'

Type declaration

  • day: "numeric"
  • month: "long"
  • weekday: "long"
  • year: "numeric"
DATE_MED: {
    day: "numeric";
    month: "short";
    year: "numeric";
} = Formats.DATE_MED

toLocaleString format like 'Oct 14, 1983'

Type declaration

  • day: "numeric"
  • month: "short"
  • year: "numeric"
DATE_MED_WITH_WEEKDAY: {
    day: "numeric";
    month: "short";
    weekday: "short";
    year: "numeric";
} = Formats.DATE_MED_WITH_WEEKDAY

toLocaleString format like 'Fri, Oct 14, 1983'

Type declaration

  • day: "numeric"
  • month: "short"
  • weekday: "short"
  • year: "numeric"
DATE_SHORT: {
    day: "numeric";
    month: "numeric";
    year: "numeric";
} = Formats.DATE_SHORT

toLocaleString format like 10/14/1983

Type declaration

  • day: "numeric"
  • month: "numeric"
  • year: "numeric"
TIME_24_SIMPLE: Pick<DateTimeFormatOptions, "hour" | "minute" | "hourCycle"> = Formats.TIME_24_SIMPLE

toLocaleString format like '09:30', always 24-hour.

TIME_24_WITH_LONG_OFFSET: Pick<DateTimeFormatOptions, "hour" | "minute" | "hourCycle" | "second" | "timeZoneName"> = Formats.TIME_24_WITH_LONG_OFFSET

toLocaleString format like '09:30:23 Eastern Daylight Time', always 24-hour.

TIME_24_WITH_SECONDS: Pick<DateTimeFormatOptions, "hour" | "minute" | "hourCycle" | "second"> = Formats.TIME_24_WITH_SECONDS

toLocaleString format like '09:30:23', always 24-hour.

TIME_24_WITH_SHORT_OFFSET: Pick<DateTimeFormatOptions, "hour" | "minute" | "hourCycle" | "second" | "timeZoneName"> = Formats.TIME_24_WITH_SHORT_OFFSET

toLocaleString format like '09:30:23 EDT', always 24-hour.

TIME_SIMPLE: {
    hour: "numeric";
    minute: "numeric";
} = Formats.TIME_SIMPLE

toLocaleString format like '09:30 AM'. Only 12-hour if the locale is.

Type declaration

  • hour: "numeric"
  • minute: "numeric"
TIME_WITH_LONG_OFFSET: {
    hour: "numeric";
    minute: "numeric";
    second: "numeric";
    timeZoneName: "long";
} = Formats.TIME_WITH_LONG_OFFSET

toLocaleString format like '09:30:23 AM Eastern Daylight Time'. Only 12-hour if the locale is.

Type declaration

  • hour: "numeric"
  • minute: "numeric"
  • second: "numeric"
  • timeZoneName: "long"
TIME_WITH_SECONDS: {
    hour: "numeric";
    minute: "numeric";
    second: "numeric";
} = Formats.TIME_WITH_SECONDS

toLocaleString format like '09:30:23 AM'. Only 12-hour if the locale is.

Type declaration

  • hour: "numeric"
  • minute: "numeric"
  • second: "numeric"
TIME_WITH_SHORT_OFFSET: {
    hour: "numeric";
    minute: "numeric";
    second: "numeric";
    timeZoneName: "short";
} = Formats.TIME_WITH_SHORT_OFFSET

toLocaleString format like '09:30:23 AM EDT'. Only 12-hour if the locale is.

Type declaration

  • hour: "numeric"
  • minute: "numeric"
  • second: "numeric"
  • timeZoneName: "short"

Accessors

  • get day(): number
  • Get the day of the month (1-30ish).

    Example

    DateTime.local(2017, 5, 25).day //=> 25
    

    Returns number

  • get daysInMonth(): number
  • Returns the number of days in this DateTime's month

    Example

    DateTime.local(2016, 2).daysInMonth //=> 29
    

    Example

    DateTime.local(2016, 3).daysInMonth //=> 31
    

    Returns number

  • get daysInYear(): number
  • Returns the number of days in this DateTime's year

    Example

    DateTime.local(2016).daysInYear //=> 366
    

    Example

    DateTime.local(2013).daysInYear //=> 365
    

    Returns number

  • get hour(): number
  • Get the hour of the day (0-23).

    Example

    DateTime.local(2017, 5, 25, 9).hour //=> 9
    

    Returns number

  • get invalidExplanation(): string | void
  • Returns an explanation of why this Duration became invalid, or null if the Duration is valid

    Returns string | void

  • get invalidReason(): string | void
  • Returns an error code if this Duration became invalid, or null if the Duration is valid

    Returns

    Returns string | void

  • get isInDST(): boolean
  • Get whether the DateTime is in a DST.

    Returns boolean

  • get isInLeapYear(): boolean
  • Returns true if this DateTime is in a leap year, false otherwise

    Example

    DateTime.local(2016).isInLeapYear //=> true
    

    Example

    DateTime.local(2013).isInLeapYear //=> false
    

    Returns boolean

  • get isOffsetFixed(): null | boolean
  • Get whether this zone's offset ever changes, as in a DST.

    Returns null | boolean

  • get isValid(): boolean
  • Returns whether the DateTime is valid. Invalid DateTimes occur when:

    • The DateTime was created from invalid calendar information, such as the 13th month or February 30
    • The DateTime was created by an operation on another invalid date

    Returns boolean

  • get locale(): undefined | string
  • Get the locale of a DateTime, such 'en-GB'. The locale is used when formatting the DateTime

    Returns undefined | string

  • get millisecond(): number
  • Get the millisecond of the second (0-999).

    Example

    DateTime.local(2017, 5, 25, 9, 30, 52, 654).millisecond //=> 654
    

    Returns number

  • get minute(): number
  • Get the minute of the hour (0-59).

    Example

    DateTime.local(2017, 5, 25, 9, 30).minute //=> 30
    

    Returns number

  • get month(): number
  • Get the month (1-12).

    Example

    DateTime.local(2017, 5, 25).month //=> 5
    

    Returns number

  • get monthLong(): null | string
  • Get the human readable long month name, such as 'October'. Defaults to the system's locale if no locale has been specified

    Example

    DateTime.local(2017, 10, 30).monthLong //=> October
    

    Returns null | string

  • get monthShort(): null | string
  • Get the human readable short month name, such as 'Oct'. Defaults to the system's locale if no locale has been specified

    Example

    DateTime.local(2017, 10, 30).monthShort //=> Oct
    

    Returns null | string

  • get numberingSystem(): undefined | Readonly<NumberingSystem>
  • Get the numbering system of a DateTime, such 'beng'. The numbering system is used when formatting the DateTime

    Returns undefined | Readonly<NumberingSystem>

  • get offset(): number
  • Get the UTC offset of this DateTime in minutes

    Example

    DateTime.now().offset //=> -240
    

    Example

    DateTime.utc().offset //=> 0
    

    Returns number

  • get offsetNameLong(): null | string
  • Get the long human name for the zone's current offset, for example "Eastern Standard Time" or "Eastern Daylight Time". Defaults to the system's locale if no locale has been specified

    Returns null | string

  • get offsetNameShort(): null | string
  • Get the short human name for the zone's current offset, for example "EST" or "EDT". Defaults to the system's locale if no locale has been specified

    Returns null | string

  • get ordinal(): number
  • Get the ordinal (meaning the day of the year)

    Example

    DateTime.local(2017, 5, 25).ordinal //=> 145
    

    Returns number

  • get outputCalendar(): undefined | CalendarSystem
  • Get the output calendar of a DateTime, such 'islamic'. The output calendar is used when formatting the DateTime

    Returns undefined | CalendarSystem

  • get quarter(): number
  • Get the quarter

    Example

    DateTime.local(2017, 5, 25).quarter //=> 2
    

    Returns number

  • get second(): number
  • Get the second of the minute (0-59).

    Example

    DateTime.local(2017, 5, 25, 9, 30, 52).second //=> 52
    

    Returns number

  • get ts(): number
  • Get the timestamp.

    Example

    DateTime.local(1970, 1, 1, 0, 0, 0, 654).ts //=> 654
    

    Returns number

  • get weekdayLong(): null | string
  • Get the human readable long weekday, such as 'Monday'. Defaults to the system's locale if no locale has been specified

    Example

    DateTime.local(2017, 10, 30).weekdayLong //=> Monday
    

    Returns null | string

  • get weekdayShort(): null | string
  • Get the human readable short weekday, such as 'Mon'. Defaults to the system's locale if no locale has been specified

    Example

    DateTime.local(2017, 10, 30).weekdayShort //=> Mon
    

    Returns null | string

  • get year(): number
  • Get the year

    Example

    DateTime.local(2017, 5, 25).year //=> 2017
    

    Returns number

  • get zone(): Readonly<Zone>
  • Get the time zone associated with this DateTime.

    Returns Readonly<Zone>

  • get zoneName(): null | string
  • Get the name of the time zone.

    Returns null | string

Methods

  • Returns the difference between two DateTimes as a Duration.

    Example

    var i1 = DateTime.fromISO('1982-05-25T09:45'),
    i2 = DateTime.fromISO('1983-10-14T10:30');
    i2.diff(i1).toObject() //=> { milliseconds: 43807500000 }
    i2.diff(i1, 'hours').toObject() //=> { hours: 12168.75 }
    i2.diff(i1, ['months', 'days']).toObject() //=> { months: 16, days: 19.03125 }
    i2.diff(i1, ['months', 'days', 'hours']).toObject() //=> { months: 16, days: 19, hours: 0.75 }

    Returns

    Parameters

    • otherDateTime: DateTime

      the DateTime to compare this one to

    • Optional unit: keyof DurationObject | (keyof DurationObject)[] = "milliseconds"

      the unit or array of units (such as 'hours' or 'days') to include in the duration.

    • opts: DurationOptions = {}

      options that affect the creation of the Duration

    Returns Duration

  • "Set" this DateTime to the end (meaning the last millisecond) of a unit of time

    Example

    DateTime.local(2014, 3, 3).endOf('month').toISO(); //=> '2014-03-31T23:59:59.999-05:00'
    

    Example

    DateTime.local(2014, 3, 3).endOf('year').toISO(); //=> '2014-12-31T23:59:59.999-05:00'
    

    Example

    DateTime.local(2014, 3, 3, 5, 30).endOf('day').toISO(); //=> '2014-03-03T23:59:59.999-05:00'
    

    Example

    DateTime.local(2014, 3, 3, 5, 30).endOf('hour').toISO(); //=> '2014-03-03T05:59:59.999-05:00'
    

    Returns

    Parameters

    • unit: keyof DurationObject

      The unit to go to the end of. Can be 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or 'millisecond'.

    Returns DateTime

  • Equality check Two DateTimes are equal iff they represent the same millisecond and have the same zone and location. To compare just the millisecond values, use +dt1 === +dt2.

    Returns

    Parameters

    Returns boolean

  • Return whether this DateTime is in the same unit of time as another DateTime. Higher-order units must also be identical for this function to return true. Note that time zones are ignored in this comparison, which compares the local calendar time. Use setZone to convert one of the dates if needed.

    Example

    DateTime.now().hasSame(otherDT, 'day'); //~> true if otherDT is in the same current calendar day
    

    Returns

    Parameters

    Returns boolean

  • Subtract a period of time to this DateTime and return the resulting DateTime See plus

    Returns

    Parameters

    • duration: number | DurationLike

      The amount to subtract. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()

    Returns DateTime

  • Add a period of time to this DateTime and return the resulting DateTime

    Adding hours, minutes, seconds, or milliseconds increases the timestamp by the right number of milliseconds. Adding days, months, or years shifts the calendar, accounting for DSTs and leap years along the way. Thus, dt.plus({ hours: 24 }) may result in a different time than dt.plus({ days: 1 }) if there's a DST shift in between.

    Example

    DateTime.now().plus(123) //~> in 123 milliseconds
    

    Example

    DateTime.now().plus({ minutes: 15 }) //~> in 15 minutes
    

    Example

    DateTime.now().plus({ days: 1 }) //~> this time tomorrow
    

    Example

    DateTime.now().plus({ days: -1 }) //~> this time yesterday
    

    Example

    DateTime.now().plus({ hours: 3, minutes: 13 }) //~> in 3 hr, 13 min
    

    Example

    DateTime.now().plus(Duration.fromObject({ hours: 3, minutes: 13 })) //~> in 3 hr, 13 min
    

    Returns

    Parameters

    • duration: number | DurationLike

      The amount to add. Either a Luxon Duration or the object argument to Duration.fromObject()

    Returns DateTime

  • "Set" the locale, numberingSystem, or outputCalendar. Returns a newly-constructed DateTime.

    Example

    DateTime.local(2017, 5, 25).reconfigure({ locale: 'en-GB' })
    

    Returns

    Parameters

    Returns DateTime

  • Returns the resolved Intl options for this DateTime. This is useful in understanding the behavior of formatting methods

    Returns

    Parameters

    • opts: {} = {}

      the same options as toLocaleString

      Returns {
          locale: string;
          numberingSystem: string;
          outputCalendar: string;
      }

      • locale: string
      • numberingSystem: string
      • outputCalendar: string
    • "Set" the locale. Returns a newly-constructed DateTime. Just a convenient alias for reconfigure({ locale })

      Example

      DateTime.local(2017, 5, 25).setLocale('en-GB')
      

      Returns

      Parameters

      • locale: string

      Returns DateTime

    • "Set" the DateTime's zone to specified zone. Returns a newly-constructed DateTime.

      By default, the setter keeps the underlying instant the same (as in, the same timestamp), but the new instance will report different local time and consider DSTs when making computations, as with plus. You may wish to use DateTime.toSystemZone and toUTC which provide simple convenience wrappers for commonly used zones.

      Returns

      Parameters

      • Optional zone: ZoneLike

        a zone identifier. As a string, that can be any IANA zone supported by the host environment, or a fixed-offset name of the form 'UTC+3', or the strings 'default', 'system' or 'utc'. You may also supply an instance of a Zone class.

      • options: SetZoneOptions = {}

        options

      Returns DateTime

    • "Set" this DateTime to the beginning of a unit of time.

      Example

      DateTime.local(2014, 3, 3).startOf('month').toISODate(); //=> '2014-03-01'
      

      Example

      DateTime.local(2014, 3, 3).startOf('year').toISODate(); //=> '2014-01-01'
      

      Example

      DateTime.local(2014, 3, 3, 5, 30).startOf('week').toISOTime(); //=> '2014-03-03', weeks always start on a Monday
      

      Example

      DateTime.local(2014, 3, 3, 5, 30).startOf('day').toISOTime(); //=> '00:00.000-05:00'
      

      Example

      DateTime.local(2014, 3, 3, 5, 30).startOf('hour').toISOTime(); //=> '05:00:00.000-05:00'
      

      Returns

      Parameters

      • unit: keyof DurationObject

        The unit to go to the beginning of. Can be 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or 'millisecond'.

      Returns DateTime

    • Returns a BSON serializable equivalent to this DateTime.

      Returns

      Returns Date

    • Returns a string representation of this DateTime formatted according to the specified format string. You may not want this. See toLocaleString for a more flexible formatting tool. For a table of tokens and their interpretations, see here. Defaults to en-US if no locale has been specified, regardless of the system's locale.

      Example

      DateTime.now().toFormat('yyyy LLL dd') //=> '2017 Apr 22'
      

      Example

      DateTime.now().setLocale('fr').toFormat('yyyy LLL dd') //=> '2017 avr. 22'
      

      Example

      DateTime.now().toFormat('yyyy LLL dd', { locale: "fr" }) //=> '2017 avr. 22'
      

      Example

      DateTime.now().toFormat("HH 'hours and' mm 'minutes'") //=> '20 hours and 55 minutes'
      

      Returns

      Parameters

      • fmt: string

        the format string

      • opts: DateTimeOptions = {}

        opts to override the configuration options on this DateTime

      Returns string

    • Returns a string representation of this DateTime appropriate for use in HTTP headers. The output is always expressed in GMT Specifically, the string conforms to RFC 1123.

      See

      https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1

      Example

      DateTime.utc(2014, 7, 13).toHTTP() //=> 'Sun, 13 Jul 2014 00:00:00 GMT'
      

      Example

      DateTime.utc(2014, 7, 13, 19).toHTTP() //=> 'Sun, 13 Jul 2014 19:00:00 GMT'
      

      Returns

      Returns null | string

    • Returns an ISO 8601-compliant string representation of this DateTime

      Example

      DateTime.utc(1983, 5, 25).toISO() //=> '1982-05-25T00:00:00.000Z'
      

      Example

      DateTime.now().toISO() //=> '2017-04-22T20:47:05.335-04:00'
      

      Example

      DateTime.now().toISO({ includeOffset: false }) //=> '2017-04-22T20:47:05.335'
      

      Example

      DateTime.now().toISO({ format: 'basic' }) //=> '20170422T204705.335-0400'
      

      Returns

      Parameters

      Returns null | string

    • Returns an ISO 8601-compliant string representation of this DateTime's date component

      Example

      DateTime.utc(1982, 5, 25).toISODate() //=> '1982-05-25'
      

      Example

      DateTime.utc(1982, 5, 25).toISODate({ format: 'basic' }) //=> '19820525'
      

      Returns

      Parameters

      • options: {
            format?: ToISOFormat;
        } = ...

        options

        • Optional format?: ToISOFormat

          choose between the basic and extended format

      Returns null | string

    • Returns an ISO 8601-compliant string representation of this DateTime's time component

      Example

      DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime() //=> '07:34:19.361Z'
      

      Example

      DateTime.utc().set({ hour: 7, minute: 34, seconds: 0, milliseconds: 0 }).toISOTime({ suppressSeconds: true }) //=> '07:34Z'
      

      Example

      DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime({ format: 'basic' }) //=> '073419.361Z'
      

      Returns

      Parameters

      Returns null | string

    • Returns an ISO 8601-compliant string representation of this DateTime's week date

      Example

      DateTime.utc(1982, 5, 25).toISOWeekDate() //=> '1982-W21-2'
      

      Returns

      Returns null | string

    • Returns a Javascript Date equivalent to this DateTime.

      Returns

      Returns Date

    • Returns an ISO 8601 representation of this DateTime appropriate for use in JSON.

      Returns

      Returns null | string

    • "Set" the DateTime's zone to the host's local zone. Returns a newly-constructed DateTime.

      Equivalent to setZone('local')

      Returns

      Returns DateTime

    • Returns a localized string representing this date. Accepts the same options as the Intl.DateTimeFormat constructor and any presets defined by Luxon, such as DateTime.DATE_FULL or DateTime.TIME_SIMPLE. The exact behavior of this method is browser-specific, but in general it will return an appropriate representation of the DateTime in the assigned locale. Defaults to the system's locale if no locale has been specified

      See

      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

      Example

      DateTime.now().toLocaleString(); //=> 4/20/2017
      

      Example

      DateTime.now().setLocale('en-gb').toLocaleString(); //=> '20/04/2017'
      

      Example

      DateTime.now().toLocaleString(DateTime.DATE_FULL); //=> 'April 20, 2017'
      

      Example

      DateTime.now().toLocaleString(DateTime.DATE_FULL, { locale: 'fr' }); //=> '28 août 2022'
      

      Example

      DateTime.now().toLocaleString(DateTime.TIME_SIMPLE); //=> '11:32 AM'
      

      Example

      DateTime.now().toLocaleString(DateTime.DATETIME_SHORT); //=> '4/20/2017, 11:32 AM'
      

      Example

      DateTime.now().toLocaleString({ weekday: 'long', month: 'long', day: '2-digit' }); //=> 'Thursday, April 20'
      

      Example

      DateTime.now().toLocaleString({ weekday: 'short', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' }); //=> 'Thu, Apr 20, 11:27 AM'
      

      Example

      DateTime.now().toLocaleString({ hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }); //=> '11:32'
      

      Returns

      Parameters

      Returns string

    • Returns the epoch milliseconds of this DateTime.

      Returns

      Returns number

    • Returns a JavaScript object with this DateTime's year, month, day, and so on.

      Example

      DateTime.now().toObject() //=> { year: 2017, month: 4, day: 22, hour: 20, minute: 49, second: 42, millisecond: 268 }
      

      Returns

      Parameters

      • opts: {
            includeConfig: boolean;
        } = ...

        options for generating the object

        • includeConfig: boolean

          include configuration attributes in the output

      Returns GregorianDateTime & Partial<LocaleOptions>

    • Returns an RFC 2822-compatible string representation of this DateTime

      Example

      DateTime.utc(2014, 7, 13).toRFC2822() //=> 'Sun, 13 Jul 2014 00:00:00 +0000'
      

      Example

      DateTime.local(2014, 7, 13).toRFC2822() //=> 'Sun, 13 Jul 2014 00:00:00 -0400'
      

      Returns

      Returns null | string

    • Returns a string representation of a this time relative to now, such as "in two days". Can only internationalize if your platform supports Intl.RelativeTimeFormat. Rounds down by default.

      Example

      DateTime.now().plus({ days: 1 }).toRelative() //=> "in 1 day"
      

      Example

      DateTime.now().setLocale("es").toRelative({ days: 1 }) //=> "dentro de 1 día"
      

      Example

      DateTime.now().plus({ days: 1 }).toRelative({ locale: "fr" }) //=> "dans 23 heures"
      

      Example

      DateTime.now().minus({ days: 2 }).toRelative() //=> "2 days ago"
      

      Example

      DateTime.now().minus({ days: 2 }).toRelative({ unit: "hours" }) //=> "48 hours ago"
      

      Example

      DateTime.now().minus({ hours: 36 }).toRelative({ round: false }) //=> "1.5 days ago"
      

      Parameters

      Returns null | string

    • Returns a string representation of this date relative to today, such as "yesterday" or "next month". Only internationalizes on platforms that supports Intl.RelativeTimeFormat.

      Example

      DateTime.now().plus({ days: 1 }).toRelativeCalendar() //=> "tomorrow"
      

      Example

      DateTime.now().setLocale("es").plus({ days: 1 }).toRelative() //=> ""mañana"
      

      Example

      DateTime.now().plus({ days: 1 }).toRelativeCalendar({ locale: "fr" }) //=> "demain"
      

      Example

      DateTime.now().minus({ days: 2 }).toRelativeCalendar() //=> "2 days ago"
      

      Parameters

      Returns null | string

    • Returns a string representation of this DateTime appropriate for use in SQL DateTime

      Example

      DateTime.utc(2014, 7, 13).toSQL() //=> '2014-07-13 00:00:00.000 Z'
      

      Example

      DateTime.local(2014, 7, 13).toSQL() //=> '2014-07-13 00:00:00.000 -04:00'
      

      Example

      DateTime.local(2014, 7, 13).toSQL({ includeOffset: false }) //=> '2014-07-13 00:00:00.000'
      

      Example

      DateTime.local(2014, 7, 13).toSQL({ includeZone: true }) //=> '2014-07-13 00:00:00.000 America/New_York'
      

      Returns

      Parameters

      Returns null | string

    • Returns a string representation of this DateTime appropriate for use in SQL Date

      Example

      DateTime.utc(2014, 7, 13).toSQLDate() //=> '2014-07-13'
      

      Returns

      Returns null | string

    • Returns a string representation of this DateTime appropriate for use in SQL Time

      Example

      DateTime.utc().toSQL() //=> '05:15:16.345'
      

      Example

      DateTime.now().toSQL() //=> '05:15:16.345 -04:00'
      

      Example

      DateTime.now().toSQL({ includeOffset: false }) //=> '05:15:16.345'
      

      Example

      DateTime.now().toSQL({ includeZone: false }) //=> '05:15:16.345 America/New_York'
      

      Returns

      Parameters

      Returns null | string

    • Returns the epoch seconds of this DateTime.

      Returns

      Returns number

    • Returns a string representation of this DateTime appropriate for debugging

      Returns

      Returns null | string

    • "Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime.

      Equivalent to setZone('utc')

      Returns

      Parameters

      • Optional offset: number = 0

        optionally, an offset from UTC in minutes

      • Optional opts: SetZoneOptions = {}

        options to pass to setZone()

      Returns DateTime

    • Returns the epoch seconds (as a whole number) of this DateTime.

      Returns

      Returns number

    • Returns the epoch milliseconds of this DateTime. Alias of toMillis

      Returns

      Returns number

    • Produce the fully expanded format token for the locale Does NOT quote characters, so quoted tokens will not round trip correctly

      Returns

      Parameters

      Returns string

    • Create a DateTime from an ISO 8601 string

      Example

      DateTime.fromISO('2016-05-25T09:08:34.123')
      

      Example

      DateTime.fromISO('2016-05-25T09:08:34.123+06:00')
      

      Example

      DateTime.fromISO('2016-05-25T09:08:34.123+06:00', {setZone: true})
      

      Example

      DateTime.fromISO('2016-05-25T09:08:34.123', {zone: 'utc'})
      

      Example

      DateTime.fromISO('2016-W05-4')
      

      Returns

      Parameters

      • text: string

        the ISO string

      • opts: DateTimeOptions = {}

        options to affect the creation

      Returns DateTime

    • Create a DateTime from a number of milliseconds since the epoch (meaning since 1 January 1970 00:00:00 UTC). Uses the default zone.

      Returns

      Parameters

      • milliseconds: number

        a number of milliseconds since 1970 UTC

      • options: DateTimeOptions = {}

        configuration options for the DateTime

      Returns DateTime

    • Create a DateTime from a JavaScript object with keys like 'year' and 'hour' with reasonable defaults.

      Example

      DateTime.fromObject({ year: 1982, month: 5, day: 25}).toISODate() //=> '1982-05-25'
      

      Example

      DateTime.fromObject({ year: 1982 }).toISODate() //=> '1982-01-01'
      

      Example

      DateTime.fromObject({ hour: 10, minute: 26, second: 6 }) //~> today at 10:26:06
      

      Example

      DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'utc' }),
      

      Example

      DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'local' })
      

      Example

      DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { }zone: 'America/New_York' })
      

      Example

      DateTime.fromObject({ weekYear: 2016, weekNumber: 2, weekday: 3 }).toISODate() //=> '2016-01-13'
      

      Returns

      Parameters

      Returns DateTime

    • Create a DateTime from an RFC 2822 string

      Example

      DateTime.fromRFC2822('25 Nov 2016 13:23:12 GMT')
      

      Example

      DateTime.fromRFC2822('Fri, 25 Nov 2016 13:23:12 +0600')
      

      Example

      DateTime.fromRFC2822('25 Nov 2016 13:23 Z')
      

      Returns

      Parameters

      • text: string

        the RFC 2822 string

      • opts: DateTimeOptions = {}

        options to affect the creation

      Returns DateTime

    • Create a DateTime from a SQL date, time, or datetime Defaults to en-US if no locale has been specified, regardless of the system's locale

      Example

      DateTime.fromSQL('2017-05-15')
      

      Example

      DateTime.fromSQL('2017-05-15 09:12:34')
      

      Example

      DateTime.fromSQL('2017-05-15 09:12:34.342')
      

      Example

      DateTime.fromSQL('2017-05-15 09:12:34.342+06:00')
      

      Example

      DateTime.fromSQL('2017-05-15 09:12:34.342 America/Los_Angeles')
      

      Example

      DateTime.fromSQL('2017-05-15 09:12:34.342 America/Los_Angeles', { setZone: true })
      

      Example

      DateTime.fromSQL('2017-05-15 09:12:34.342', { zone: 'America/Los_Angeles' })
      

      Example

      DateTime.fromSQL('09:12:34.342')
      

      Returns

      Parameters

      • text: string

        the string to parse

      • opts: DateTimeOptions = {}

        options to affect the creation

      Returns DateTime

    • Create a DateTime from a number of seconds since the epoch (meaning since 1 January 1970 00:00:00 UTC). Uses the default zone.

      Returns

      Parameters

      • seconds: number

        a number of seconds since 1970 UTC

      • options: DateTimeOptions = {}

        configuration options for the DateTime

      Returns DateTime

    • Create an invalid DateTime.

      Returns

      Parameters

      • reason: string | Invalid

        simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent

      • Optional explanation: string

        longer explanation, may include parameters and other useful debugging information

      Returns DateTime

    • Check if an object is an instance of DateTime. Works across context boundaries

      Returns

      Parameters

      • o: unknown

      Returns o is DateTime

    • Create a local DateTime

      Example

      [year] - The calendar year. If omitted (as in, call local() with no arguments), the current time will be used

      Example

      {number} [month=1] - The month, 1-indexed
      

      Example

      {number} [day=1] - The day of the month, 1-indexed
      

      Example

      {number} [hour=0] - The hour of the day, in 24-hour time
      

      Example

      {number} [minute=0] - The minute of the hour, meaning a number between 0 and 59
      

      Example

      {number} [second=0] - The second of the minute, meaning a number between 0 and 59
      

      Example

      {number} [millisecond=0] - The millisecond of the second, meaning a number between 0 and 999
      

      Example

      DateTime.local()                                //~> now
      

      Example

      DateTime.local({ zone: "America/New_York" })    //~> now, in US east coast time
      

      Example

      DateTime.local(2017)                            //~> 2017-01-01T00:00:00
      

      Example

      DateTime.local(2017, 3)                         //~> 2017-03-01T00:00:00
      

      Example

      DateTime.local(2017, 3, 12, { locale: "fr" })     //~> 2017-03-12T00:00:00, with a French locale
      

      Example

      DateTime.local(2017, 3, 12, 5)                  //~> 2017-03-12T05:00:00
      

      Example

      DateTime.local(2017, 3, 12, 5, { zone: "utc" }) //~> 2017-03-12T05:00:00, in UTC
      

      Example

      DateTime.local(2017, 3, 12, 5, 45, 10)          //~> 2017-03-12T05:45:10
      

      Example

      DateTime.local(2017, 3, 12, 5, 45, 10, 765)     //~> 2017-03-12T05:45:10.765
      

      Returns

      Parameters

      • Rest ...args: [DateTimeOptions] | number[] | (number | DateTimeOptions)[]

        The date values (year, month, etc.) and/or the configuration options for the DateTime

      Returns DateTime

    • Return the max of several date times

      Returns

      the max DateTime, or undefined if called with no arguments

      Parameters

      • Rest ...dateTimes: []

        the DateTimes from which to choose the maximum

      Returns void

    • Parameters

      Returns DateTime

    • Return the min of several date times

      Returns

      the min DateTime, or undefined if called with no arguments

      Parameters

      • Rest ...dateTimes: []

        the DateTimes from which to choose the minimum

      Returns void

    • Parameters

      Returns DateTime

    • Create a DateTime for the current instant, in the system's time zone.

      Use Settings to override these default values if needed.

      Example

      DateTime.now().toISO() //~> now in the ISO format
      

      Returns

      Returns DateTime

    • Create a DateTime in UTC

      Example

      [year] - The calendar year. If omitted (as in, call utc() with no arguments), the current time will be used

      Example

      {number} [month=1] - The month, 1-indexed
      

      Example

      {number} [day=1] - The day of the month
      

      Example

      {number} [hour=0] - The hour of the day, in 24-hour time
      

      Example

      {number} [minute=0] - The minute of the hour, meaning a number between 0 and 59
      

      Example

      {number} [second=0] - The second of the minute, meaning a number between 0 and 59
      

      Example

      {number} [millisecond=0] - The millisecond of the second, meaning a number between 0 and 999
      

      Example

      {Object} options - configuration options for the DateTime
      

      Example

      {string} [options.locale] - a locale to set on the resulting DateTime instance
      

      Example

      {string} [options.outputCalendar] - the output calendar to set on the resulting DateTime instance
      

      Example

      {string} [options.numberingSystem] - the numbering system to set on the resulting DateTime instance
      

      Example

      DateTime.utc()                            //~> now
      

      Example

      DateTime.utc(2017)                        //~> 2017-01-01T00:00:00Z
      

      Example

      DateTime.utc(2017, 3)                     //~> 2017-03-01T00:00:00Z
      

      Example

      DateTime.utc(2017, 3, 12)                 //~> 2017-03-12T00:00:00Z
      

      Example

      DateTime.utc(2017, 3, 12, 5)              //~> 2017-03-12T05:00:00Z
      

      Example

      DateTime.utc(2017, 3, 12, 5, 45)          //~> 2017-03-12T05:45:00Z
      

      Example

      DateTime.utc(2017, 3, 12, 5, 45, { locale: "fr" } )       //~> 2017-03-12T05:45:00Z with a French locale
      

      Example

      DateTime.utc(2017, 3, 12, 5, 45, 10)      //~> 2017-03-12T05:45:10Z
      

      Example

      DateTime.utc(2017, 3, 12, 5, 45, 10, 765, { locale: "fr }) //~> 2017-03-12T05:45:10.765Z
      

      Returns

      Parameters

      • Rest ...args: [DateTimeOptions] | number[] | (number | DateTimeOptions)[]

        The date values (year, month, etc.) and/or the configuration options for the DateTime

      Returns DateTime

    Generated using TypeDoc