Skip to main content

DateTime

ฦฌ DateTime: Format["YYYY-MM-DDThh:mm-ss[.sss]"] & string

An Iso.DateTime represents a calendar date and wall-clock time, with a precision in milliseconds, and without any time zone.

For use cases that require a time zone, especially using arithmetic or other derived values, consider using Iso.ZonedDateTime instead because that type automatically adjusts for Daylight Saving Time. A Iso.DateTime can be converted to a Iso.ZonedDateTime using a Iso.TimeZone.

Iso.Date, Iso.Time, Iso.YearMonth, and Iso.MonthDay all carry less information and should be used when complete information is not required.

A Iso.DateTime can be converted into any of the types mentioned above using conversion methods like toZonedDateTime or toDate.

Because Iso.DateTime does not represent an exact point in time, most date/time use cases are better handled using exact time types like Iso.ZonedDateTime and Iso.Instant. But there are cases where Iso.DateTime is the correct type to use:

  • Representing timezone-specific events where the time zone is not stored together with the date/time data. In this case, Iso.DateTime is an intermediate step before converting to/from Iso.ZonedDateTime or Iso.Instant using the separate time zone. Examples:
    • When the time zone is stored separately in a separate database column or a per-user setting.
    • Implicit time zones, e.g. stock exchange data that is always America/New_York
    • Interacting with poorly-designed legacy systems that record data in the server's non-UTC time zone.
  • Passing data to/from a component that is unaware of time zones, e.g. a UI date/time picker.
  • Modeling events that happen at the same local time in every time zone. For example, the British Commonwealth observes a two minute silence every November 11th at 11:00AM in local time.
  • When time zone is irrelevant, e.g. a sleep tracking device that only cares about the local time you went to sleep and woke up, regardless of where in the world you are.
  • Parsing local time from ISO 8601 strings like 2020-04-09T16:08-08:00 that have a numeric offset without an IANA time zone like America/Los_Angeles. These strings can also be parsed by Iso.Instant, but to parse the local date and time then dateTimeFns.from is required.
  • Performing arithmetic that deliberately ignores DST. Example: in a day-planner UI, the visual height of a meeting may be the same even if DST skips or repeats an hour. To learn more about time zones and DST best practices, visit Time Zones and Resolving Ambiguity.

Defined in

iso-types.ts:97

now

โ–ธ now(timeZone?): DateTime

This method gets the current calendar date and wall-clock time according to the system settings. Optionally a time zone can be given in which the time is computed, instead of the current system time zone.

Parameters

NameTypeDescription
timeZone?stringThe time zone to get the current date and time in, as a string. If not given, the current system time zone will be used.

Returns

DateTime

an Iso.DateTime string representing the date and time.

Defined in

types/IDateTimeFns.ts:17


fromNumbers

โ–ธ fromNumbers(year, month, day, hour?, minute?, second?, millisecond?): DateTime

Parameters

NameType
yearnumber
monthnumber
daynumber
hour?number
minute?number
second?number
millisecond?number

Returns

DateTime

Defined in

types/IDateTimeFns.ts:18


isValid

โ–ธ isValid(dateTime): dateTime is DateTime

Parameters

NameType
dateTimeunknown

Returns

dateTime is DateTime

Defined in

types/IDateTimeFns.ts:27


assertIsValid

โ–ธ assertIsValid(dateTime): asserts dateTime is DateTime

Parameters

NameType
dateTimeunknown

Returns

asserts dateTime is DateTime

Defined in

types/IDateTimeFns.ts:28


getYear

โ–ธ getYear(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:29


getMonth

โ–ธ getMonth(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:30


getDay

โ–ธ getDay(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:31


getHour

โ–ธ getHour(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:32


getMinute

โ–ธ getMinute(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:33


getSecond

โ–ธ getSecond(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:34


getMillisecond

โ–ธ getMillisecond(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:35


getDayOfWeek

โ–ธ getDayOfWeek(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:36


getDayOfYear

โ–ธ getDayOfYear(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:37


getWeekOfYear

โ–ธ getWeekOfYear(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:38


getDaysInYear

โ–ธ getDaysInYear(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:39


getDaysInMonth

โ–ธ getDaysInMonth(dateTime): number

Parameters

NameType
dateTimeDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:40


getInLeapYear

โ–ธ getInLeapYear(dateTime): boolean

Parameters

NameType
dateTimeDateTime

Returns

boolean

Defined in

types/IDateTimeFns.ts:41


with

โ–ธ with(dateTime, temporalDateTimeLike, options?): DateTime

Parameters

NameType
dateTimeDateTime
temporalDateTimeLikeObject
temporalDateTimeLike.day?number
temporalDateTimeLike.hour?number
temporalDateTimeLike.millisecond?number
temporalDateTimeLike.minute?number
temporalDateTimeLike.month?number
temporalDateTimeLike.second?number
temporalDateTimeLike.year?number
options?Object
options.overflow?TemporalOverflow

Returns

DateTime

Defined in

types/IDateTimeFns.ts:42


withTime

โ–ธ withTime(dateTime, time?): DateTime

Parameters

NameType
dateTimeDateTime
time?Time

Returns

DateTime

Defined in

types/IDateTimeFns.ts:57


withDate

โ–ธ withDate(dateTime, temporalDate): DateTime

Parameters

NameType
dateTimeDateTime
temporalDateDate

Returns

DateTime

Defined in

types/IDateTimeFns.ts:58


add

โ–ธ add(dateTime, temporalDurationLike, options?): DateTime

Parameters

NameType
dateTimeDateTime
temporalDurationLikeDuration | { years?: number ; months?: number ; weeks?: number ; days?: number ; hours?: number ; minutes?: number ; seconds?: number ; milliseconds?: number }
options?Object
options.overflow?TemporalOverflow

Returns

DateTime

Defined in

types/IDateTimeFns.ts:59


subtract

โ–ธ subtract(dateTime, temporalDurationLike, options?): DateTime

Parameters

NameType
dateTimeDateTime
temporalDurationLikeDuration | { years?: number ; months?: number ; weeks?: number ; days?: number ; hours?: number ; minutes?: number ; seconds?: number ; milliseconds?: number }
options?Object
options.overflow?TemporalOverflow

Returns

DateTime

Defined in

types/IDateTimeFns.ts:75


until

โ–ธ until(from, until, options?): Duration

Parameters

NameType
fromDateTime
untilDateTime
options?Object
options.largestUnit?TemporalSingularUnit | TemporalPluralUnit | "auto"
options.smallestUnit?TemporalSingularUnit | TemporalPluralUnit
options.roundingIncrement?number
options.roundingMode?TemporalRoundingMode

Returns

Duration

Defined in

types/IDateTimeFns.ts:91


since

โ–ธ since(dateTime, other, options?): Duration

Parameters

NameType
dateTimeDateTime
otherDateTime
options?Object
options.largestUnit?TemporalSingularUnit | TemporalPluralUnit | "auto"
options.smallestUnit?TemporalSingularUnit | TemporalPluralUnit
options.roundingIncrement?number
options.roundingMode?TemporalRoundingMode

Returns

Duration

Defined in

types/IDateTimeFns.ts:101


round

โ–ธ round(dateTime, options): DateTime

Parameters

NameType
dateTimeDateTime
optionsObject
options.smallestUnit"day" | "hour" | "minute" | "second" | "millisecond" | TemporalPluralUnit
options.roundingIncrement?number
options.roundingMode?TemporalRoundingMode

Returns

DateTime

Defined in

types/IDateTimeFns.ts:111


equals

โ–ธ equals(dateTime, other): boolean

Parameters

NameType
dateTimeDateTime
otherDateTime

Returns

boolean

Defined in

types/IDateTimeFns.ts:119


isEqual

โ–ธ isEqual(dateTime, other): boolean

alias for equals

Parameters

NameType
dateTimeDateTime
otherDateTime

Returns

boolean

Defined in

types/IDateTimeFns.ts:123


isBefore

โ–ธ isBefore(dateTime, other): boolean

Parameters

NameType
dateTimeDateTime
otherDateTime

Returns

boolean

Defined in

types/IDateTimeFns.ts:124


isAfter

โ–ธ isAfter(dateTime, other): boolean

Parameters

NameType
dateTimeDateTime
otherDateTime

Returns

boolean

Defined in

types/IDateTimeFns.ts:125


isEqualOrBefore

โ–ธ isEqualOrBefore(dateTime, other): boolean

Parameters

NameType
dateTimeDateTime
otherDateTime

Returns

boolean

Defined in

types/IDateTimeFns.ts:126


isEqualOrAfter

โ–ธ isEqualOrAfter(dateTime, other): boolean

Parameters

NameType
dateTimeDateTime
otherDateTime

Returns

boolean

Defined in

types/IDateTimeFns.ts:127


toZonedDateTime

โ–ธ toZonedDateTime(dateTime, timeZone, options?): ZonedDateTime

Parameters

NameType
dateTimeDateTime
timeZonestring
options?Object
options.disambiguationTemporalDisambiguation

Returns

ZonedDateTime

Defined in

types/IDateTimeFns.ts:128


toDate

โ–ธ toDate(dateTime): Date

Parameters

NameType
dateTimeDateTime

Returns

Date

Defined in

types/IDateTimeFns.ts:133


toYearMonth

โ–ธ toYearMonth(dateTime): YearMonth

Parameters

NameType
dateTimeDateTime

Returns

YearMonth

Defined in

types/IDateTimeFns.ts:134


toMonthDay

โ–ธ toMonthDay(dateTime): MonthDay

Parameters

NameType
dateTimeDateTime

Returns

MonthDay

Defined in

types/IDateTimeFns.ts:135


toTime

โ–ธ toTime(dateTime): Time

Parameters

NameType
dateTimeDateTime

Returns

Time

Defined in

types/IDateTimeFns.ts:136


getFields

โ–ธ getFields(dateTime): DateTimeSlots

Parameters

NameType
dateTimeDateTime

Returns

DateTimeSlots

Defined in

types/IDateTimeFns.ts:137


from

โ–ธ from(item, options?): DateTime

Parameters

NameType
itemany
options?Object
options.overflowTemporalOverflow

Returns

DateTime

Defined in

types/IDateTimeFns.ts:138


compare

โ–ธ compare(one, two): number

Parameters

NameType
oneDateTime
twoDateTime

Returns

number

Defined in

types/IDateTimeFns.ts:139


format

โ–ธ format(dateTime, formatString): string

summary Format the ZonedDateTime.

description Return the formatted ZonedDateTime string in the given format.

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

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

Accepted patterns:

UnitPatternResult examplesNotes
EraG..GGGAD, BC
GGGGAnno Domini, Before Christ1
GGGGGA, B
Calendar yeary44, 1, 1900, 20174
yo44th, 1st, 0th, 17th4,5
yy44, 01, 00, 174
yyy044, 001, 1900, 20174
yyyy0044, 0001, 1900, 20174
yyyyy...2,4
Extended yearu-43, 0, 1, 1900, 20174
uu-43, 01, 1900, 20174
uuu-043, 001, 1900, 20174
uuuu-0043, 0001, 1900, 20174
uuuuu...2,4
QuarterQ1, 2, 3, 4
Qo1st, 2nd, 3rd, 4th5
QQ01, 02, 03, 04
QQQQ1, Q2, Q3, Q4
QQQQ1st quarter, 2nd quarter, ...1
QQQQQ1, 2, 3, 43
MonthM1, 2, ..., 12
Mo1st, 2nd, ..., 12th5
MM01, 02, ..., 12
MMMJan, Feb, ..., Dec
MMMMJanuary, February, ..., December1
MMMMMJ, F, ..., D
ISO week of yearI1, 2, ..., 535
Io1st, 2nd, ..., 53th5
II01, 02, ..., 535
Day of monthd1, 2, ..., 31
do1st, 2nd, ..., 31st5
dd01, 02, ..., 31
ISO day of weeki1, 2, 3, ..., 75
io1st, 2nd, ..., 7th5
ii01, 02, ..., 075
iiiMon, Tue, Wed, ..., Sun5
iiiiMonday, Tuesday, ..., Sunday1,5
iiiiiM, T, W, T, F, S, S5
iiiiiiMo, Tu, We, Th, Fr, Sa, Su5
AM, PMa..aaAM, PM
aaaam, pm
aaaaa.m., p.m.1
aaaaaa, p
AM, PM, noon, midnightb..bbAM, PM, noon, midnight
bbbam, pm, noon, midnight
bbbba.m., p.m., noon, midnight1
bbbbba, p, n, mi
Flexible day periodB..BBBat night, in the morning, ...
BBBBat night, in the morning, ...1
BBBBBat night, in the morning, ...
Hour [1-12]h1, 2, ..., 11, 12
ho1st, 2nd, ..., 11th, 12th5
hh01, 02, ..., 11, 12
Hour [0-23]H0, 1, 2, ..., 23
Ho0th, 1st, 2nd, ..., 23rd5
HH00, 01, 02, ..., 23
Hour [0-11]K1, 2, ..., 11, 0
Ko1st, 2nd, ..., 11th, 0th5
KK01, 02, ..., 11, 00
Hour [1-24]k24, 1, 2, ..., 23
ko24th, 1st, 2nd, ..., 23rd5
kk24, 01, 02, ..., 23
Minutem0, 1, ..., 59
mo0th, 1st, ..., 59th5
mm00, 01, ..., 59
Seconds0, 1, ..., 59
so0th, 1st, ..., 59th5
ss00, 01, ..., 59
Fraction of secondS0, 1, ..., 9
SS00, 01, ..., 99
SSS000, 001, ..., 999
SSSS...2
Long localized dateP04/29/14535
PPApr 29, 14535
PPPApril 29th, 14535
PPPPFriday, April 29th, 14531,5
Long localized timep12:00 AM5
pp12:00:00 AM5
Combination of date and timePp04/29/1453, 12:00 AM5
PPppApr 29, 1453, 12:00:00 AM5

Notes:

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

    format("2017-11-06", 'MMM') //=> 'Nov'

    format("2017-11-06", 'MMMM') //=> 'November'

    format("2017-11-06", 'MMMMM') //=> 'N'

    format("2017-11-06", 'MMMMMM') //=> 'November'

    format("2017-11-06", 'MMMMMMM') //=> 'November'

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

    format("2017-11-06", 'yyyyyyyy') //=> '00002017'

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

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

    Yearyu
    AC 111
    BC 110
    BC 22-1

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

    Yearyyuu
    10101
    141414
    37676376
    1453531453
  5. These patterns are not in the Unicode Technical Standard #35:

    • i: ISO day of week
    • I: ISO week of year
    • o: ordinal number modifier
    • P: long localized date
    • p: long localized time
  • Characters are escaped using single quote symbols (').

example

// Represent 11 February 2014 in middle-endian format:
var result = format("2014-02-11T00:00:00.000", 'MM/dd/yyyy')
//=> '02/11/2014'

example

// Escape string by single quote characters:
var result = format("2014-02-11T15:00:00.000", "h 'o''clock'")
//=> "3 o'clock"

Parameters

NameTypeDescription
dateTimeDateTimethe original date
formatStringstring-

Returns

string

the formatted date string

Defined in

types/IDateTimeFns.ts:309


chain

โ–ธ chain(dateTime): IDateTimeChain

Parameters

NameType
dateTimeDateTime

Returns

IDateTimeChain

Defined in

types/IDateTimeFns.ts:310