DEV Community

TK
TK

Posted on

tc39 proposal-tempral: Time-zone conversion

What is Temporal

A global Object that acts as a top-level namespace (like Math), that brings a modern date/time API to the ECMAScript language.

Temporal documentation

It is on Stage 3 proposal at the time of publishing this article.

Time-zone conversion

What I would like to show you in this article is one of the killer methods that should be super useful, Time-zone conversion๐Ÿ—บ.
(Plus, it does support DST-safe arithmetic!! ๐Ÿคฏ)

const instant = Temporal.Instant.from('2022-02-27T02:04Z');
instant.toString(); // => '2022-02-27T02:04:00Z'

const currentTz = Temporal.Now.timeZone();
currentTz.toString(); // => 'Asia/Tokyo'

const jpDateTime = new Temporal.ZonedDateTime(
  instant.epochNanoseconds, 
  currentTz, 
  Temporal.Calendar.from('iso8601')
);
jpDateTime.toString(); 
// => '2022-02-27T11:04:00+09:00[Asia/Tokyo]'

const laDateTime = new Temporal.ZonedDateTime(
  instant.epochNanoseconds,
  Temporal.TimeZone.from('America/Los_Angeles'),
  Temporal.Calendar.from('iso8601')
);
laDateTime.toString() 
// => '2022-02-26T18:04:00-08:00[America/Los_Angeles]'
Enter fullscreen mode Exit fullscreen mode

How I tried Temporal

Open this official doc and used the dev-tool console ๐ŸŽฎ

how-to-try

Reference

For more details, please check out the official documents ๐Ÿ˜‰

Oldest comments (0)