How to Outsmart Time: Building Futuristic JavaScript Apps Using Temporal

Rate this content
Bookmark

For close to 25 years now, JavaScript developers have suffered at the hands of time: the Date object. We have tried all sorts of solutions from using popular libraries like Moment.js all the way to handling dates and times on the backend.

Now it's time to show "time" who is boss. With the Temporal API stable at Stage 3 and the polyfill ready for production use, let me show you how to harness the power of this delightful API in order to build powerful JavaScript applications that handle dates and times like we always wished we could. We shall also discuss the orthogonal features being worked on in the JavaScript Intl API and find out how these play along with each other and come together to form a comprehensive set of APIs that allow us to build state of the art date and time components in our applications.

This talk has been presented at JSNation Live 2021, check out the latest edition of this JavaScript Conference.

FAQ

Temporal is a state-of-the-art, date-time handling proposal in JavaScript designed to address deficiencies in existing date-time APIs by providing a more robust and flexible solution. It includes support for different calendars, time zones, and more precise date and time calculations.

The Temporal API offers various classes like Instant, PlainDateTime, and ZonedDateTime, supporting precise and fuzzy concepts of time. It includes local calendar support, custom time zones, and features for complex use cases that were previously unaddressed, making it a comprehensive solution for date-time handling.

Developers can begin using Temporal in their JavaScript applications as it has reached Stage 3, meaning the specification text has been approved and general design finalized. Implementation in browsers and polyfills is underway, allowing developers to use Temporal in production environments.

A TC39 delegate is a member of the technical committee that manages the ECMAScript specification, under which JavaScript is standardized. TC39 delegates are crucial for proposing new features like Temporal, reviewing them, and pushing them through stages until they become part of the JavaScript language.

Temporal provides a native, more efficient solution for date-time handling directly within the JavaScript language, potentially reducing dependency on third-party libraries like Moment.js, which can lead to smaller bundle sizes and better performance in web applications.

Temporal supports a variety of calendars and custom time zones, allowing for accurate time calculations across different regions and calendar systems. This flexibility makes it suitable for global applications that need to handle diverse date and time requirements.

Reaching Stage 3 indicates that all major design decisions for Temporal have been settled, the specification text is approved, and the feature is ready for implementation. This stage is critical as it means Temporal is stable enough for developers to start using in production environments.

Yes, Temporal is designed to be a comprehensive replacement for the existing Date object in JavaScript, providing more powerful and flexible functionalities to handle all aspects of date and time more effectively.

Ujjwal Sharma
Ujjwal Sharma
25 min
11 Jun, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Temporal is a proposal in JavaScript that brings an ergonomic API for building futuristic JavaScript applications. It is now at stage 3 and implementers will start implementing it in different polyfills and browsers. Temporal includes classes for working with dates, times, time zones, calendars, and durations. The talk covers building an invoice calculator using Temporal, calculating durations between date times, creating and manipulating durations, understanding time relativity and rounding, and rewriting methods using Temporal. The speaker expresses gratitude to various contributors and provides a sandbox for exploration.

1. Introduction to Temporal

Short description:

Hello and welcome, everyone. Today we're going to talk about building futuristic JavaScript applications in Tempo. The Temporal proposal in JavaScript brings an ergonomic API with a special focus on common and complex use cases, including local calendar support. Temporal is now at stage 3.

Hello and welcome, everyone. I'm Ujwal. You can find me on the internet at whyzacooking. If you want to run into me on Twitter or GitHub, I'll have a terrible how terrible this talk is or want to talk about Tempo or TZ39 stuff. But today we're going to talk about building futuristic JavaScript applications in Tempo. So let's get into it.

Because I'm a self-obsessed person. Let's first talk about me. So, I have a compiler's hacker, Regalia. For those of you who don't know, it's a free software consultancy in Spain. We work on a bunch of cool things. We work on browsers, compilers, standards like the ones I'm going to talk to you about today and a bunch of cool Linux stuff. I am also a TC39 delegate, which is why I'm talking to you about these things. I am one of the editors of Ecma 402 and the champion of the proposal that we talked about today and also happen to be one of the co-collaborators of O.J.S.

Just to give you a little recap about the history of the whole temporal thing, what the buzz is all about, if you've used the date object in JavaScript ever to do any sort of date time handling, you would know that it is severely outdated. It has serious issues. It dates at least back to 1985, which is before I was born, so how is that for a metaphor? But it needs a lot of repairs, which means that if you're writing any serious date time handling application, you're probably using some popular third-party libraries. They're pretty popular these days in the JavaScript ecosystem. You're either using Moment or Luxon date functions, and, well, they fill a really important role in the JavaScript language, but there's still quite a few problems. There's still deficiencies, if you follow JavaScript drama not so long ago, Lighthouse, which is a tool that sort of gives you performance tips regarding a JavaScript code, started flagging applications that use Moment, asking them to use something that would use less bundle size. So that's, for example, one of the problems that cannot be solved in the user space, among a bunch of others that I'm going to talk to you about. But because of all these problems, we realized that something needed to be done on the programming language level itself. Thus I bring in Temporal.

So Temporal is a state-of-the-art, daytime handling proposal in JavaScript. It brings you not only to the present, but, as you will see in the next few slides, right into the future. One of the most interesting challenges that we had when designing the Temporal API was certainly about going to sort of conflicting things, right? There's JavaScript of proposals that you see that are more specifically geared towards advanced users, you know them when you see them. There's others that are more sort of geared towards beginners, they make your life easier, but not as much if you have been writing this language for like 10 years, and in Temporal, the beauty of the Temporal proposal is that it tries to pair an ergonomic API with a special focus on some of the most common use cases alongside some really powerful features that accommodate some of the most complex use cases, some of the use cases that have never been accommodated so far. Things including local calendar support, so including calendars like the Hebrew or Islamic calendar or custom time zones of calendars. These are truly features that have been so far overlooked. So temporal, the thing that I talked about just now, is now stage 3, which is great news.

2. Exploring Temporal

Short description:

Temporal is now at stage 3. Implementers will start implementing temporal in different polyfills and browsers. JavaScript developers can start using temporal in their applications, including production. Temporal includes classes such as instant, plain date time, plain time, plain date, and zone date. It also encompasses time zones, calendars, and durations. Instant represents absolute points in time, while plain types deal with world clock time and calendar dates. Zone date time combines an instant and a time zone.

Except we don't really know what that means. So just to sum it up real quick, in the interest of time, it means that all the tiny details have been discussed. You've been sitting around in our chairs for a long time and realized that we've basically exhausted everything that we could have done while sitting in our chairs. So the specification text has been approved, the committee is now satisfied with the general design, and now the idea is to start implementing and using temporal.

So for implementers like us, that means that we will start implementing things in different polyfills. We will start implementing temporal in different browsers, and for you amazing JavaScript developers, it means that you can start using temporal in your applications, including production. Just to give you a quick idea of the huge, honestly, API surface of temporal, you have a number of these classes that we're going to talk about in the next few slides. Just to briefly go over them, there's instant, which happens to exist in the exact time space. So it knows the exact time that it's happened since the epoch. And then there's a bunch of fuzzy time types. So there's plain date time, plain time, plain date. They happen to be all subsets of plain date time. So they talk about more fuzzy notions of time and dates. So, hey, 80 a.m. in the morning doesn't really matter how many exact nanoseconds have passed, right? And then there's a zone date type, which is sort of the ubermensch of all these types. It encompasses all the different use cases that we so far offer, and it can be completely replaced. It can replace totally date objects, but hopefully you would realize that you need something much less powerful. There's time zones and calendars, which sort of play between these. And then there's durations, which are used mostly for map.

To quickly summarize this, because this is going fast, but hopefully I'd be able to at least draw your attention in the span of 20 minutes, instance represent absolute points in time. So something as granular and as boring, as you may say, as a number of nanoseconds that have passed since the Epoch, which in this case is the Unix Epoch, 1970. And then there's the plane types, plane date time and friends who deal with the regular world clock time and calendar dates. So, for example, if you're building a fitness tracker, it doesn't really matter if I'm in Moscow or in Amsterdam. All that matters is that I wake up at 8 a.m. and eat a lot of bagels. Don't go for a run. Calendars refer to human calendars, so, you know, in common understanding, it would refer to the Gregorian calendar, which is pretty popular. It may refer to the more obscure calendars, but still used throughout the world, like the Buddhist calendar or the Hebrew calendar, Julian calendar, and then there's time zones that refer to an exact offset, so something like plus one hour from UTC or a more fuzzy human time zone, so something like Europe slash Amsterdam, which may represent different points of time depending on what time you're talking about and if it's summer or not. Zone date time, as I said, is the combination of an instant and a time zone, so it takes the boring instant and it pairs it with the time zone to project a date time from it, right? So if you know which time zone you're in and you know the exact number of nanoseconds that have passed, then you can give any information, you can tell me what time of the day it is and so on. It's the most powerful thing.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
RemixConf EU discussed full stack components and their benefits, such as marrying the backend and UI in the same file. The talk demonstrated the implementation of a combo box with search functionality using Remix and the Downshift library. It also highlighted the ease of creating resource routes in Remix and the importance of code organization and maintainability in full stack components. The speaker expressed gratitude towards the audience and discussed the future of Remix, including its acquisition by Shopify and the potential for collaboration with Hydrogen.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
WebAssembly enables optimizing JavaScript performance for different environments by deploying the JavaScript engine as a portable WebAssembly module. By making JavaScript on WebAssembly fast, instances can be created for each request, reducing latency and security risks. Initialization and runtime phases can be improved with tools like Wiser and snapshotting, resulting in faster startup times. Optimizing JavaScript performance in WebAssembly can be achieved through techniques like ahead-of-time compilation and inline caching. WebAssembly usage is growing outside the web, offering benefits like isolation and portability. Build sizes and snapshotting in WebAssembly depend on the application, and more information can be found on the Mozilla Hacks website and Bike Reliance site.
Webpack in 5 Years?
JSNation 2022JSNation 2022
26 min
Webpack in 5 Years?
Top Content
In the last 10 years, Webpack has shaped the way we develop web applications by introducing code splitting, co-locating style sheets and assets with JavaScript modules, and enabling bundling for server-side processing. Webpack's flexibility and large plugin system have also contributed to innovation in the ecosystem. The initial configuration for Webpack can be overwhelming, but it is necessary due to the complexity of modern web applications. In larger scale applications, there are performance problems in Webpack due to issues with garbage collection, leveraging multiple CPUs, and architectural limitations. Fixing problems in Webpack has trade-offs, but a rewrite could optimize architecture and fix performance issues.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
There is a need for a standard library of APIs for JavaScript runtimes, as there are currently multiple ways to perform fundamental tasks like base64 encoding. JavaScript runtimes have historically lacked a standard library, causing friction and difficulty for developers. The idea of a small core has both benefits and drawbacks, with some runtimes abusing it to limit innovation. There is a misalignment between Node and web browsers in terms of functionality and API standards. The proposal is to involve browser developers in conversations about API standardization and to create a common standard library for JavaScript runtimes.

Workshops on related topic

Master JavaScript Patterns
JSNation 2024JSNation 2024
145 min
Master JavaScript Patterns
Featured Workshop
Adrian Hajdin
Adrian Hajdin
During this workshop, participants will review the essential JavaScript patterns that every developer should know. Through hands-on exercises, real-world examples, and interactive discussions, attendees will deepen their understanding of best practices for organizing code, solving common challenges, and designing scalable architectures. By the end of the workshop, participants will gain newfound confidence in their ability to write high-quality JavaScript code that stands the test of time.
Points Covered:
1. Introduction to JavaScript Patterns2. Foundational Patterns3. Object Creation Patterns4. Behavioral Patterns5. Architectural Patterns6. Hands-On Exercises and Case Studies
How It Will Help Developers:
- Gain a deep understanding of JavaScript patterns and their applications in real-world scenarios- Learn best practices for organizing code, solving common challenges, and designing scalable architectures- Enhance problem-solving skills and code readability- Improve collaboration and communication within development teams- Accelerate career growth and opportunities for advancement in the software industry
Integrating LangChain with JavaScript for Web Developers
React Summit 2024React Summit 2024
92 min
Integrating LangChain with JavaScript for Web Developers
Featured Workshop
Vivek Nayyar
Vivek Nayyar
Dive into the world of AI with our interactive workshop designed specifically for web developers. "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" offers a unique opportunity to bridge the gap between AI and web development. Despite the prominence of Python in AI development, the vast potential of JavaScript remains largely untapped. This workshop aims to change that.Throughout this hands-on session, participants will learn how to leverage LangChain—a tool designed to make large language models more accessible and useful—to build dynamic AI agents directly within JavaScript environments. This approach opens up new possibilities for enhancing web applications with intelligent features, from automated customer support to content generation and beyond.We'll start with the basics of LangChain and AI models, ensuring a solid foundation even for those new to AI. From there, we'll dive into practical exercises that demonstrate how to integrate these technologies into real-world JavaScript projects. Participants will work through examples, facing and overcoming the challenges of making AI work seamlessly on the web.This workshop is more than just a learning experience; it's a chance to be at the forefront of an emerging field. By the end, attendees will not only have gained valuable skills but also created AI-enhanced features they can take back to their projects or workplaces.Whether you're a seasoned web developer curious about AI or looking to expand your skillset into new and exciting areas, "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" is your gateway to the future of web development. Join us to unlock the potential of AI in your web projects, making them smarter, more interactive, and more engaging for users.
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.
Testing Web Applications Using Cypress
TestJS Summit - January, 2021TestJS Summit - January, 2021
173 min
Testing Web Applications Using Cypress
WorkshopFree
Gleb Bahmutov
Gleb Bahmutov
This workshop will teach you the basics of writing useful end-to-end tests using Cypress Test Runner.
We will cover writing tests, covering every application feature, structuring tests, intercepting network requests, and setting up the backend data.
Anyone who knows JavaScript programming language and has NPM installed would be able to follow along.
React Server Components Unleashed: A Deep Dive into Next-Gen Web Development
React Day Berlin 2023React Day Berlin 2023
149 min
React Server Components Unleashed: A Deep Dive into Next-Gen Web Development
Workshop
Maurice de Beijer
Maurice de Beijer
Get ready to supercharge your web development skills with React Server Components! In this immersive, 3-hour workshop, we'll unlock the full potential of this revolutionary technology and explore how it's transforming the way developers build lightning-fast, efficient web applications.
Join us as we delve into the exciting world of React Server Components, which seamlessly blend server-side rendering with client-side interactivity for unparalleled performance and user experience. You'll gain hands-on experience through practical exercises, real-world examples, and expert guidance on how to harness the power of Server Components in your own projects.
Throughout the workshop, we'll cover essential topics, including:- Understanding the differences between Server and Client Components- Implementing Server Components to optimize data fetching and reduce JavaScript bundle size- Integrating Server and Client Components for a seamless user experience- Strategies for effectively passing data between components and managing state- Tips and best practices for maximizing the performance benefits of React Server Components
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher