docs.rodeo

MDN Web Docs mirror

Temporal

{{JSRef}} {{SeeCompatTable}} 

The Temporal object enables date and time management in various scenarios, including built-in time zone and calendar representation, wall-clock time conversions, arithmetics, formatting, and more. It is designed as a full replacement for the {{jsxref("Date")}}  object.

Description

Unlike most global objects, Temporal is not a constructor. You cannot use it with the new operator or invoke the Temporal object as a function. All properties and methods of Temporal are static (just like the {{jsxref("Math")}}  object).

Temporal has an intricate and powerful API. It exposes over 200 utility methods via several classes, so it could appear very complex. We will provide a high-level overview of how these APIs are related to each other.

Background and concepts

JavaScript has had the {{jsxref("Date")}}  object for handling date and time since its first days. However, the Date API is based on the poorly designed java.util.Date class from Java, which was replaced in the early 2010s; but, because of JavaScript’s goal of backward compatibility, Date sticks around in the language.

The important lesson to preface the whole introduction is that date handling is complex. Most of the problems of Date are fixable by adding more methods, but a fundamental design flaw remains: it exposes so many methods on the same object that developers are often confused about what to use, leading to unexpected pitfalls. A well-designed API not only needs to do more, but also should do less with each level of abstraction, because preventing misuse is as important as enabling use cases.

Date objects wear two hats simultaneously:

Time zones underlie a significant number of date-related bugs. When interacting with a Date via the “combination of components” model, the time can only be in two time zones: UTC and local (device), and there’s no way to specify an arbitrary time zone. Also lacking is the concept of “no time zone”: this is known as a calendar date (for dates) or wall-clock time (for times), which is a time you “read off a calendar or clock”. For example, if you are setting a daily wake up alarm, you will want to set it to “8:00AM” regardless of whether it is daylight saving time or not, whether you have traveled to a different time zone, etc.

A second feature lacking from Date is a calendar system. Most people may be familiar with the Gregorian calendar, where there are two eras, BC and AD; there are 12 months; each month has a different number of days; there’s a leap year every 4 years; and so on. However, some of these concepts may not apply when you are working with another calendar system, such as the Hebrew calendar, the Chinese calendar, the Japanese calendar, etc. With Date, you can only work with the Gregorian calendar model.

There are many other undesirable legacies about Date, such as all setters being mutating (which often causes unwanted side effects), the date time string format being impossible to parse in a consistent way, etc. In the end, the best solution is to build a new API from scratch, which is what Temporal is.

API overview

Temporal is a namespace, like {{jsxref("Intl")}} . It contains several classes and namespaces, each of which is designed to handle a specific aspect of date and time management. The classes can be grouped as such:

Furthermore, there’s also another utility namespace, {{jsxref("Temporal.Now")}} , which provides methods for getting the current time in various formats.

Shared class interface

There are many classes in the Temporal namespace, but they share many similar methods. The following table lists all methods of each class (except conversion methods):

Instant ZonedDateTime PlainDateTime PlainDate PlainTime PlainYearMonth PlainMonthDay
Construction `{{jsxref("Temporal/Instant/Instant", "Instant()")}}` 
`{{jsxref("Temporal/Instant/from", "Instant.from()")}}` 
`{{jsxref("Temporal/Instant/fromEpochMilliseconds", "Instant.fromEpochMilliseconds()")}}` 
`{{jsxref("Temporal/Instant/fromEpochNanoseconds", "Instant.fromEpochNanoseconds()")}}` 
`{{jsxref("Temporal/ZonedDateTime/ZonedDateTime", "ZonedDateTime()")}}` 
`{{jsxref("Temporal/ZonedDateTime/from", "ZonedDateTime.from()")}}` 
`{{jsxref("Temporal/PlainDateTime/PlainDateTime", "PlainDateTime()")}}` 
`{{jsxref("Temporal/PlainDateTime/from", "PlainDateTime.from()")}}` 
`{{jsxref("Temporal/PlainDate/PlainDate", "PlainDate()")}}` 
`{{jsxref("Temporal/PlainDate/from", "PlainDate.from()")}}` 
`{{jsxref("Temporal/PlainTime/PlainTime", "PlainTime()")}}` 
`{{jsxref("Temporal/PlainTime/from", "PlainTime.from()")}}` 
`{{jsxref("Temporal/PlainYearMonth/PlainYearMonth", "PlainYearMonth()")}}` 
`{{jsxref("Temporal/PlainYearMonth/from", "PlainYearMonth.from()")}}` 
`{{jsxref("Temporal/PlainMonthDay/PlainMonthDay", "PlainMonthDay()")}}` 
`{{jsxref("Temporal/PlainMonthDay/from", "PlainMonthDay.from()")}}` 
Updater N/A `{{jsxref("Temporal/ZonedDateTime/with", "with()")}}` 
`{{jsxref("Temporal/ZonedDateTime/withCalendar", "withCalendar()")}}` 
`{{jsxref("Temporal/ZonedDateTime/withTimeZone", "withTimeZone()")}}` 
`{{jsxref("Temporal/ZonedDateTime/withPlainTime", "withPlainTime()")}}` 
`{{jsxref("Temporal/PlainDateTime/with", "with()")}}` 
`{{jsxref("Temporal/PlainDateTime/withCalendar", "withCalendar()")}}` 
`{{jsxref("Temporal/PlainDateTime/withPlainTime", "withPlainTime()")}}` 
`{{jsxref("Temporal/PlainDate/with", "with()")}}` 
`{{jsxref("Temporal/PlainDate/withCalendar", "withCalendar()")}}` 
`{{jsxref("Temporal/PlainTime/with", "with()")}}`  `{{jsxref("Temporal/PlainYearMonth/with", "with()")}}`  `{{jsxref("Temporal/PlainMonthDay/with", "with()")}}` 
Arithmetic `{{jsxref("Temporal/Instant/add", "add()")}}` 
`{{jsxref("Temporal/Instant/subtract", "subtract()")}}` 
`{{jsxref("Temporal/Instant/since", "since()")}}` 
`{{jsxref("Temporal/Instant/until", "until()")}}` 
`{{jsxref("Temporal/ZonedDateTime/add", "add()")}}` 
`{{jsxref("Temporal/ZonedDateTime/subtract", "subtract()")}}` 
`{{jsxref("Temporal/ZonedDateTime/since", "since()")}}` 
`{{jsxref("Temporal/ZonedDateTime/until", "until()")}}` 
`{{jsxref("Temporal/PlainDateTime/add", "add()")}}` 
`{{jsxref("Temporal/PlainDateTime/subtract", "subtract()")}}` 
`{{jsxref("Temporal/PlainDateTime/since", "since()")}}` 
`{{jsxref("Temporal/PlainDateTime/until", "until()")}}` 
`{{jsxref("Temporal/PlainDate/add", "add()")}}` 
`{{jsxref("Temporal/PlainDate/subtract", "subtract()")}}` 
`{{jsxref("Temporal/PlainDate/since", "since()")}}` 
`{{jsxref("Temporal/PlainDate/until", "until()")}}` 
`{{jsxref("Temporal/PlainTime/add", "add()")}}` 
`{{jsxref("Temporal/PlainTime/subtract", "subtract()")}}` 
`{{jsxref("Temporal/PlainTime/since", "since()")}}` 
`{{jsxref("Temporal/PlainTime/until", "until()")}}` 
`{{jsxref("Temporal/PlainYearMonth/add", "add()")}}` 
`{{jsxref("Temporal/PlainYearMonth/subtract", "subtract()")}}` 
`{{jsxref("Temporal/PlainYearMonth/since", "since()")}}` 
`{{jsxref("Temporal/PlainYearMonth/until", "until()")}}` 
N/A
Rounding `{{jsxref("Temporal/Instant/round", "round()")}}`  `{{jsxref("Temporal/ZonedDateTime/round", "round()")}}`  `{{jsxref("Temporal/PlainDateTime/round", "round()")}}`  N/A `{{jsxref("Temporal/PlainTime/round", "round()")}}`  N/A N/A
Comparison `{{jsxref("Temporal/Instant/equals", "equals()")}}` 
`{{jsxref("Temporal/Instant/compare", "Instant.compare()")}}` 
`{{jsxref("Temporal/ZonedDateTime/equals", "equals()")}}` 
`{{jsxref("Temporal/ZonedDateTime/compare", "ZonedDateTime.compare()")}}` 
`{{jsxref("Temporal/PlainDateTime/equals", "equals()")}}` 
`{{jsxref("Temporal/PlainDateTime/compare", "PlainDateTime.compare()")}}` 
`{{jsxref("Temporal/PlainDate/equals", "equals()")}}` 
`{{jsxref("Temporal/PlainDate/compare", "PlainDate.compare()")}}` 
`{{jsxref("Temporal/PlainTime/equals", "equals()")}}` 
`{{jsxref("Temporal/PlainTime/compare", "PlainTime.compare()")}}` 
`{{jsxref("Temporal/PlainYearMonth/equals", "equals()")}}` 
`{{jsxref("Temporal/PlainYearMonth/compare", "PlainYearMonth.compare()")}}` 
`{{jsxref("Temporal/PlainMonthDay/equals", "equals()")}}` 
Serialization `{{jsxref("Temporal/Instant/toJSON", "toJSON()")}}` 
`{{jsxref("Temporal/Instant/toLocaleString", "toLocaleString()")}}` 
`{{jsxref("Temporal/Instant/toString", "toString()")}}` 
`{{jsxref("Temporal/Instant/valueOf", "valueOf()")}}` 
`{{jsxref("Temporal/ZonedDateTime/toJSON", "toJSON()")}}` 
`{{jsxref("Temporal/ZonedDateTime/toLocaleString", "toLocaleString()")}}` 
`{{jsxref("Temporal/ZonedDateTime/toString", "toString()")}}` 
`{{jsxref("Temporal/ZonedDateTime/valueOf", "valueOf()")}}` 
`{{jsxref("Temporal/PlainDateTime/toJSON", "toJSON()")}}` 
`{{jsxref("Temporal/PlainDateTime/toLocaleString", "toLocaleString()")}}` 
`{{jsxref("Temporal/PlainDateTime/toString", "toString()")}}` 
`{{jsxref("Temporal/PlainDateTime/valueOf", "valueOf()")}}` 
`{{jsxref("Temporal/PlainDate/toJSON", "toJSON()")}}` 
`{{jsxref("Temporal/PlainDate/toLocaleString", "toLocaleString()")}}` 
`{{jsxref("Temporal/PlainDate/toString", "toString()")}}` 
`{{jsxref("Temporal/PlainDate/valueOf", "valueOf()")}}` 
`{{jsxref("Temporal/PlainTime/toJSON", "toJSON()")}}` 
`{{jsxref("Temporal/PlainTime/toLocaleString", "toLocaleString()")}}` 
`{{jsxref("Temporal/PlainTime/toString", "toString()")}}` 
`{{jsxref("Temporal/PlainTime/valueOf", "valueOf()")}}` 
`{{jsxref("Temporal/PlainYearMonth/toJSON", "toJSON()")}}` 
`{{jsxref("Temporal/PlainYearMonth/toLocaleString", "toLocaleString()")}}` 
`{{jsxref("Temporal/PlainYearMonth/toString", "toString()")}}` 
`{{jsxref("Temporal/PlainYearMonth/valueOf", "valueOf()")}}` 
`{{jsxref("Temporal/PlainMonthDay/toJSON", "toJSON()")}}` 
`{{jsxref("Temporal/PlainMonthDay/toLocaleString", "toLocaleString()")}}` 
`{{jsxref("Temporal/PlainMonthDay/toString", "toString()")}}` 
`{{jsxref("Temporal/PlainMonthDay/valueOf", "valueOf()")}}` 

The following table summarizes which properties are available on each class, giving you a sense of what information each class can represent.

Instant ZonedDateTime PlainDateTime PlainDate PlainTime PlainYearMonth PlainMonthDay
Calendar N/A `{{jsxref("Temporal/ZonedDateTime/calendarId", "calendarId")}}`  `{{jsxref("Temporal/PlainDateTime/calendarId", "calendarId")}}`  `{{jsxref("Temporal/PlainDate/calendarId", "calendarId")}}`  N/A `{{jsxref("Temporal/PlainYearMonth/calendarId", "calendarId")}}`  `{{jsxref("Temporal/PlainMonthDay/calendarId", "calendarId")}}` 
Year-related N/A `{{jsxref("Temporal/ZonedDateTime/era", "era")}}` 
`{{jsxref("Temporal/ZonedDateTime/eraYear", "eraYear")}}` 
`{{jsxref("Temporal/ZonedDateTime/year", "year")}}` 
`{{jsxref("Temporal/ZonedDateTime/inLeapYear", "inLeapYear")}}` 
`{{jsxref("Temporal/ZonedDateTime/monthsInYear", "monthsInYear")}}` 
`{{jsxref("Temporal/ZonedDateTime/daysInYear", "daysInYear")}}` 
`{{jsxref("Temporal/PlainDateTime/era", "era")}}` 
`{{jsxref("Temporal/PlainDateTime/eraYear", "eraYear")}}` 
`{{jsxref("Temporal/PlainDateTime/year", "year")}}` 
`{{jsxref("Temporal/PlainDateTime/inLeapYear", "inLeapYear")}}` 
`{{jsxref("Temporal/PlainDateTime/monthsInYear", "monthsInYear")}}` 
`{{jsxref("Temporal/PlainDateTime/daysInYear", "daysInYear")}}` 
`{{jsxref("Temporal/PlainDate/era", "era")}}` 
`{{jsxref("Temporal/PlainDate/eraYear", "eraYear")}}` 
`{{jsxref("Temporal/PlainDate/year", "year")}}` 
`{{jsxref("Temporal/PlainDate/inLeapYear", "inLeapYear")}}` 
`{{jsxref("Temporal/PlainDate/monthsInYear", "monthsInYear")}}` 
`{{jsxref("Temporal/PlainDate/daysInYear", "daysInYear")}}` 
N/A `{{jsxref("Temporal/PlainYearMonth/era", "era")}}` 
`{{jsxref("Temporal/PlainYearMonth/eraYear", "eraYear")}}` 
`{{jsxref("Temporal/PlainYearMonth/year", "year")}}` 
`{{jsxref("Temporal/PlainYearMonth/inLeapYear", "inLeapYear")}}` 
`{{jsxref("Temporal/PlainYearMonth/monthsInYear", "monthsInYear")}}` 
`{{jsxref("Temporal/PlainYearMonth/daysInYear", "daysInYear")}}` 
N/A
Month-related N/A `{{jsxref("Temporal/ZonedDateTime/month", "month")}}` 
`{{jsxref("Temporal/ZonedDateTime/monthCode", "monthCode")}}` 
`{{jsxref("Temporal/ZonedDateTime/daysInMonth", "daysInMonth")}}` 
`{{jsxref("Temporal/PlainDateTime/month", "month")}}` 
`{{jsxref("Temporal/PlainDateTime/monthCode", "monthCode")}}` 
`{{jsxref("Temporal/PlainDateTime/daysInMonth", "daysInMonth")}}` 
`{{jsxref("Temporal/PlainDate/month", "month")}}` 
`{{jsxref("Temporal/PlainDate/monthCode", "monthCode")}}` 
`{{jsxref("Temporal/PlainDate/daysInMonth", "daysInMonth")}}` 
N/A `{{jsxref("Temporal/PlainYearMonth/month", "month")}}` 
`{{jsxref("Temporal/PlainYearMonth/monthCode", "monthCode")}}` 
`{{jsxref("Temporal/PlainYearMonth/daysInMonth", "daysInMonth")}}` 
`{{jsxref("Temporal/PlainMonthDay/monthCode", "monthCode")}}` 
Week-related N/A `{{jsxref("Temporal/ZonedDateTime/weekOfYear", "weekOfYear")}}` 
`{{jsxref("Temporal/ZonedDateTime/yearOfWeek", "yearOfWeek")}}` 
`{{jsxref("Temporal/ZonedDateTime/daysInWeek", "daysInWeek")}}` 
`{{jsxref("Temporal/PlainDateTime/weekOfYear", "weekOfYear")}}` 
`{{jsxref("Temporal/PlainDateTime/yearOfWeek", "yearOfWeek")}}` 
`{{jsxref("Temporal/PlainDateTime/daysInWeek", "daysInWeek")}}` 
`{{jsxref("Temporal/PlainDate/weekOfYear", "weekOfYear")}}` 
`{{jsxref("Temporal/PlainDate/yearOfWeek", "yearOfWeek")}}` 
`{{jsxref("Temporal/PlainDate/daysInWeek", "daysInWeek")}}` 
N/A N/A N/A
Day-related N/A `{{jsxref("Temporal/ZonedDateTime/day", "day")}}` 
`{{jsxref("Temporal/ZonedDateTime/dayOfWeek", "dayOfWeek")}}` 
`{{jsxref("Temporal/ZonedDateTime/dayOfYear", "dayOfYear")}}` 
`{{jsxref("Temporal/PlainDateTime/day", "day")}}` 
`{{jsxref("Temporal/PlainDateTime/dayOfWeek", "dayOfWeek")}}` 
`{{jsxref("Temporal/PlainDateTime/dayOfYear", "dayOfYear")}}` 
`{{jsxref("Temporal/PlainDate/day", "day")}}` 
`{{jsxref("Temporal/PlainDate/dayOfWeek", "dayOfWeek")}}` 
`{{jsxref("Temporal/PlainDate/dayOfYear", "dayOfYear")}}` 
N/A N/A `{{jsxref("Temporal/PlainMonthDay/day", "day")}}` 
Time components N/A `{{jsxref("Temporal/ZonedDateTime/hour", "hour")}}` 
`{{jsxref("Temporal/ZonedDateTime/minute", "minute")}}` 
`{{jsxref("Temporal/ZonedDateTime/second", "second")}}` 
`{{jsxref("Temporal/ZonedDateTime/millisecond", "millisecond")}}` 
`{{jsxref("Temporal/ZonedDateTime/microsecond", "microsecond")}}` 
`{{jsxref("Temporal/ZonedDateTime/nanosecond", "nanosecond")}}` 
`{{jsxref("Temporal/PlainDateTime/hour", "hour")}}` 
`{{jsxref("Temporal/PlainDateTime/minute", "minute")}}` 
`{{jsxref("Temporal/PlainDateTime/second", "second")}}` 
`{{jsxref("Temporal/PlainDateTime/millisecond", "millisecond")}}` 
`{{jsxref("Temporal/PlainDateTime/microsecond", "microsecond")}}` 
`{{jsxref("Temporal/PlainDateTime/nanosecond", "nanosecond")}}` 
N/A `{{jsxref("Temporal/PlainTime/hour", "hour")}}` 
`{{jsxref("Temporal/PlainTime/minute", "minute")}}` 
`{{jsxref("Temporal/PlainTime/second", "second")}}` 
`{{jsxref("Temporal/PlainTime/millisecond", "millisecond")}}` 
`{{jsxref("Temporal/PlainTime/microsecond", "microsecond")}}` 
`{{jsxref("Temporal/PlainTime/nanosecond", "nanosecond")}}` 
N/A N/A
Time zone N/A `{{jsxref("Temporal/ZonedDateTime/timeZoneId", "timeZoneId")}}` 
`{{jsxref("Temporal/ZonedDateTime/offset", "offset")}}` 
`{{jsxref("Temporal/ZonedDateTime/offsetNanoseconds", "offsetNanoseconds")}}` 
`{{jsxref("Temporal/ZonedDateTime/hoursInDay", "hoursInDay")}}` 
`{{jsxref("Temporal/ZonedDateTime/getTimeZoneTransition", "getTimeZoneTransition()")}}` 
`{{jsxref("Temporal/ZonedDateTime/startOfDay", "startOfDay()")}}` 
N/A N/A N/A N/A N/A
Epoch time `{{jsxref("Temporal/Instant/epochMilliseconds", "epochMilliseconds")}}` 
`{{jsxref("Temporal/Instant/epochNanoseconds", "epochNanoseconds")}}` 
`{{jsxref("Temporal/ZonedDateTime/epochMilliseconds", "epochMilliseconds")}}` 
`{{jsxref("Temporal/ZonedDateTime/epochNanoseconds", "epochNanoseconds")}}` 
N/A N/A N/A N/A N/A

Conversion between classes

The table below summarizes all conversion methods that exist on each class.

How to convert from...
Instant ZonedDateTime PlainDateTime PlainDate PlainTime PlainYearMonth PlainMonthDay
to...Instant/`{{jsxref("Temporal/ZonedDateTime/toInstant", "toInstant()")}}` Convert to ZonedDateTime first
ZonedDateTime`{{jsxref("Temporal/Instant/toZonedDateTimeISO", "toZonedDateTimeISO()")}}` /`{{jsxref("Temporal/PlainDateTime/toZonedDateTime", "toZonedDateTime()")}}` `{{jsxref("Temporal/PlainDate/toZonedDateTime", "toZonedDateTime()")}}` `{{jsxref("Temporal/PlainDate/toZonedDateTime", "PlainDate#toZonedDateTime()")}}`  (pass as argument)Convert to PlainDate first
PlainDateTimeConvert to ZonedDateTime first`{{jsxref("Temporal/ZonedDateTime/toPlainDateTime", "toPlainDateTime()")}}` /`{{jsxref("Temporal/PlainDate/toPlainDateTime", "toPlainDateTime()")}}` `{{jsxref("Temporal/PlainDate/toPlainDateTime", "PlainDate#toPlainDateTime()")}}`  (pass as argument)
PlainDate`{{jsxref("Temporal/ZonedDateTime/toPlainDate", "toPlainDate()")}}` `{{jsxref("Temporal/PlainDateTime/toPlainDate", "toPlainDate()")}}` /No overlap in information`{{jsxref("Temporal/PlainYearMonth/toPlainDate", "toPlainDate()")}}` `{{jsxref("Temporal/PlainMonthDay/toPlainDate", "toPlainDate()")}}` 
PlainTime`{{jsxref("Temporal/ZonedDateTime/toPlainTime", "toPlainTime()")}}` `{{jsxref("Temporal/PlainDateTime/toPlainTime", "toPlainTime()")}}` No overlap in information/No overlap in information
PlainYearMonthConvert to PlainDate first`{{jsxref("Temporal/PlainDate/toPlainYearMonth", "toPlainYearMonth()")}}` No overlap in information/Convert to PlainDate first
PlainMonthDay`{{jsxref("Temporal/PlainDate/toPlainMonthDay", "toPlainMonthDay()")}}` Convert to PlainDate first/

With these tables, you should have a basic idea of how to navigate the Temporal API.

Calendars

A calendar is a way to organize days, typically into periods of weeks, months, years, and eras. Most of the world uses the Gregorian calendar, but there are many other calendars in use, especially in religious and cultural contexts. By default, all calendar-aware Temporal objects use the ISO 8601 calendar system, which is based on the Gregorian calendar and defines additional week-numbering rules. Intl.supportedValuesOf() lists most of the calendars likely to be supported by browsers. Here we provide a brief overview of how calendar systems are formed to help you internalize what factors may vary between calendars.

There are three prominent periodic events on Earth: its rotation around the sun (365.242 days for one revolution), the moon’s rotation around the Earth (29.53 days from new moon to new moon), and its rotation around its axis (24 hours from sunrise to sunrise). Every culture has the same measure of a “day”, which is 24 hours. Occasional changes such as daylight saving time are not part of the calendar, but are part of the time zone’s information.

In Temporal, every date under one calendar system is uniquely identified by three components: year, month, and day. While year is typically a positive integer, it may also be zero or negative, and increases monotonically with time. The year 1 (or 0, if it exists) is known as the calendar epoch, and is arbitrary for each calendar. month is a positive integer that increments by 1 every time, starting at 1 and ending at date.monthsInYear, then resetting back to 1 as the year advances. day is also a positive integer, but it may not start at 1 or increment by 1 every time, because political changes may cause days to be skipped or repeated. But in general, day monotonically increases and resets as the month advances.

In addition to year, a year can also be uniquely identified by the combination of era and eraYear, for calendars that use eras. For example, the Gregorian calendar uses the era “CE” (Common Era) and “BCE” (Before Common Era), and the year -1 is the same as { era: "bce", eraYear: 1 }. era is a lowercase string, and eraYear is an arbitrary integer that may be zero or negative, or even decrease with time (usually for the oldest era).

[!NOTE] Always use era and eraYear as a pair; don’t use one property without also using the other. In addition, to avoid conflicts, don’t combine year and era/eraYear when designating a year. Pick one year representation and use it consistently.

Be careful of the following incorrect assumptions about years:

In addition to month, a month in a year can also be uniquely identified by the monthCode. monthCode usually maps to the month’s name, but month does not. For example, in the case of lunisolar calendars, two months with the same monthCode, where one belongs to a leap year and the other one does not, will have different month values if they come after the leap month, due to the insertion of an extra month.

[!NOTE] To avoid conflicts, don’t combine month and monthCode when designating a month. Pick one month representation and use it consistently. month is more useful if you need the order of months in a year (e.g., when looping through the months), while monthCode is more useful if you need the name of the month (e.g., when storing birthdays).

Be careful of the following incorrect assumptions about months:

In addition to day (which is a month-based index), a day in a year can also be uniquely identified by the dayOfYear. dayOfYear is a positive integer that increments by 1 every time, starting at 1 and ending at date.daysInYear.

The concept of a “week” is not connected with any astronomical event, but is a cultural construct. While the most common length is 7 days, weeks can also have 4, 5, 6, 8, or more days — or even lack a fixed number of days altogether. To get the specific number of days of the week of a date, use the date’s daysInWeek. Temporal identifies weeks by the combination of weekOfYear and yearOfWeek. weekOfYear is a positive integer that increments by 1 every time, starting at 1, then resetting back to 1 as the year advances. yearOfWeek is generally the same as year, but may be different at the start or end of each year, because one week may cross two years, and yearOfWeek picks one of the two years based on the calendar’s rules.

[!NOTE] Always use weekOfYear and yearOfWeek as a pair; don’t use weekOfYear and year.

Be careful of the following incorrect assumptions about weeks:

RFC 9557 format

All Temporal classes can be serialized and deserialized using the format specified in RFC 9557, which is based on ISO 8601 / RFC 3339. The format, in its full form, is as follows (spaces are only for readability and should not be present in the actual string):

YYYY-MM-DD T HH:mm:ss.sssssssss Z/±HH:mm [time_zone_id] [u-ca=calendar_id]

Different classes have different requirements for the presence of each component, so you will find a section titled “RFC 9557 format” in each class’s documentation, which specifies the format recognized by that class.

This is very similar to the date time string format used by {{jsxref("Date")}} , which is also based on ISO 8601. The main addition is the ability to specify micro- and nanosecond components, and the ability to specify the time zone and calendar system.

Representable dates

All Temporal objects that represent a specific calendar date impose a similar limit on the range of representable dates, which is ±108 days (inclusive) from the Unix epoch, or the range of instants from -271821-04-20T00:00:00 to +275760-09-13T00:00:00. This is the same range as valid dates. More specifically:

The Temporal objects will refuse to construct an instance representing a date/time beyond this limit. This includes:

Static properties

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN