Skip to content

Latest commit

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Temporal Proposal

Provides standard objects and functions for working with dates and times.

Champions

Status

This proposal is currently stage 1

Overview / Motivation

Date has been a long time pain point in ECMAScript. This proposes temporal, a built in module that brings a modern date time API to the ECMAScript language. For a detailed breakdown of motivations see: Fixing JavaScript Date

Principles:

  • All temporal APIs are non-mutating. All temporal objects are effectively immutable.
  • All date values are based on the Proleptic Gregorian Calendar. Other calendar systems are out-of-scope for this proposal. However, we will consider how future APIs may interact with this one such that extending it to support other calendars may be possible in a future proposal.
  • All time-of-day values are based on a standard 24-hour clock.
  • Leap seconds are not represented.

Overview of Standard Objects in the temporal module

Objects representing Civil Time

Object nameDescriptionExample
CivilDateA date without any time or time zone reference.2017-12-31
CivilTimeA time-of-day without any date or time zone reference.17:00:00
CivilDateTimeA date and a time without any time zone reference.2017-12-31T12:00:00

Objects representing Absolute Time

Object nameDescriptionExample
InstantA point on the universal timeline, typically represented in UTC.2017-12-31T00:00:00Z
ZonedInstantA point on the universal timeline, with an associated time zone.2017‑12‑31T09:00:00+09:00[Asia/Tokyo]

Note that the time zone of a ZonedInstant can be any of:

  • Coordinated Universal Time, indicated by the string 'UTC'
  • The system local time zone, indicated by the string 'SYSTEM'
  • A fixed offset from UTC, indicated by a string in '±HH:MM' or '±HHMM' format
  • A Zone or Link name from the IANA time zone database, as also listed here.

Because a fixed offset is supported, there is no need for a separate OffsetDateTime type.


Scenario-Based Examples

TBD


Object: CivilDate

Represents a whole day, as a date on the proleptic Gregorian calendar.

Constructor

newCivilDate(year,month,day)

Parameters

  • year : Integer value representing the year.
  • month : Integer value representing the month, from 1 through 12.
  • day : Integer value representing the day, from 1 through the number of days for the given month and year, which may be 28, 29, 30, or 31.

Properties

letyear=civilDate.year;letmonth=civilDate.month;letday=civilDate.day;

Functions

letcivilDate2=civilDate1.plus({months: 1});letcivilDateTime=civilDate.withTime(time);

Object: CivilTime

Represents a position on a 24-hour clock.

Constructor

newCivilTime(hour,minute[[[,second],millisecond],nanosecond])

Parameters

  • hour : Integer value representing the hour of the day, from 0 through 23.
  • minute : Integer value representing the minute within the hour, from 0 through 59.
  • second : Optional. Integer value representing the second within the minute, from 0 through 59.
  • millisecond : Optional. Integer value representing the millisecond within the second, from 0 through 999.
  • nanosecond : Optional. Integer value representing the nanosecond within the millisecond, from 0 through 999999.

Properties

lethour=civilTime.hour;letminute=civilTime.minute;letsecond=civilTime.second;letmillisecond=civilTime.millisecond;letnanosecond=civilTime.nanosecond;

Functions

letcivilTime2=civilTime1.plus({hours: 2,minutes: 4});letcivilDateTime=civilTime.withDate(date);

Object: CivilDateTime

Represents a whole day, and the position within that day.

Constructor

newCivilDateTime(year,month,day,hour,minute[,second[,millisecond[,nanosecond]]])

Parameters

  • year : Integer value representing the year.
  • month : Integer value representing the month, from 1 through 12.
  • day : Integer value representing the day, from 1 through the number of days for the given month and year, which may be 28, 29, 30, or 31.
  • hour : Integer value representing the hour of the day, from 0 through 23.
  • minute : Integer value representing the minute within the hour, from 0 through 59.
  • second : Optional. Integer value representing the second within the minute, from 0 through 59.
  • millisecond : Optional. Integer value representing the millisecond within the second, from 0 through 999.
  • nanosecond : Optional. Integer value representing the nanosecond within the millisecond, from 0 through 999999.

Properties

letyear=civilDateTime.year;letmonth=civilDateTime.month;letday=civilDateTime.day;lethour=civilDateTime.hour;letminute=civilDateTime.minute;letsecond=civilDateTime.second;letmillisecond=civilDateTime.millisecond;letnanosecond=civilDateTime.nanosecond;

Functions

letcivilDateTime=CivilDateTime.from(date,time);letcivilDateTime2=civilDateTime1.plus({days: 3,hours: 4,minutes: 2,seconds: 12});letcivilDate=civilDateTime.toCivilDate();letcivilTime=civilDateTime.toCivilTime();letzonedInstant=civilDateTime.withZone(timeZone[,options]);

Object: Instant

Represents an absolute point in time. Counted as number of nanoseconds from 1970-01-01T00:00:00.000000000Z.

Constructor

newInstant(milliseconds[,nanoseconds])

Parameters

  • milliseconds : Integer value representing the number of milliseconds elapsed from 1970-01-01 00:00:00.000 UTC, without regarding leap seconds.
  • nanoseconds : Optional. Integer value representing the nanosecond within the millisecond.

Properties

letmilliseconds=instant.milliseconds;letnanoseconds=instant.nanoseconds;

Functions

letzonedInstant=instant.withZone(timeZone);

Object: ZonedInstant

Represents an absolute point in time, with an associated time zone.

Constructor

newZonedInstant(instant,timeZone)

Properties

letmilliseconds=zonedInstant.milliseconds;letnanoseconds=zonedInstant.nanoseconds;lettimeZone=zonedInstant.timeZone;

Functions

letcivilDateTime=zonedInstant.toCivilDateTime();letcivilDate=zonedInstant.toCivilDate();letcivilTime=zonedInstant.toCivilTime();letinstant=zonedInstant.toInstant();

with function (all civil objects)

Allows the user to create a new instance of any temporal object with new date-part values.

letmyCivilDate=newCivilDate(2016,2,29);letnewCivilDate=myDate.with({year: 2017,month: 3});//results in civil date with value 2017-03-29

plus function (all objects)

Returns a new temporal object with the specified date parts added. Units will be added in order of size, descending.

letmyCivilDate=newCivilDate(2016,2,29);letnewCivilDate=myCivilDate.plus({years: 1,months: 2});//results in civil date with value 2017-4-28

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors