DEV Community

Zaid Rehman
Zaid Rehman

Posted on

Stage 3: Temporal

Provides new standard objects and functions for working with dates and times, solving all previous issues with Date.

Date has been a long-standing pain point in ECMAScript. Temporal, is a global Object that acts as a top-level namespace (like Math), that brings a modern date/time API to the ECMAScript language.
For a detailed look at some of the problems with Date, and the motivations for Temporal, see: Fixing JavaScript Date.

Temporal fixes these problems by:

  • Providing easy-to-use APIs for date and time computations
  • First-class support for all time zones, including DST-safe arithmetic
  • Dealing only with objects representing fixed dates and times
  • Parsing a strictly specified string format
  • Supporting non-Gregorian calendars

Object Relationship
Object Relationship Of Temporal

Code examples

Temporal.Now

Temporal.Now.instant() - get the exact time since Unix epoch
Temporal.Now.timeZone() - get the current system time zone

Temporal.Instant

const instant = Temporal.Instant.from('1969-07-20T20:17Z');
instant.toString();  // => '1969-07-20T20:17:00Z'
instant.epochMilliseconds;  // => -14182980000
Enter fullscreen mode Exit fullscreen mode

Temporal.PlainDate

const date = Temporal.PlainDate.from({ year:  2006, month:  8, day:  24  });  // => 2006-08-24 
date.year;  // => 2006 
date.inLeapYear;  // => false 
date.toString();  // => '2006-08-24'
Enter fullscreen mode Exit fullscreen mode

Cookbook
https://tc39.es/proposal-temporal/docs/cookbook.html

Polyfills
https://www.npmjs.com/package/@js-temporal/polyfill

Top comments (0)