Temporal: Modern Dates and Times in JavaScript

Rate this content
Bookmark

Ever been fooled by one of the many pitfalls of the JS Date object? Do you bundle time zone data? Then check out Temporal! Temporal is a TC39 proposal currently being implemented in JS engines, that brings better date and time handling, and time zone awareness, to JS. This talk is a tour of what you need to know about Temporal, walking through one simple, one medium, and one complicated task each solved with Temporal.

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

FAQ

Temporal introduces several types including Instant, PlainDate, PlainTime, PlainYearMonth, PlainMonthDay, ZonedDateTime, and Duration, each serving different purposes in date and time representation.

Temporal introduces the ZonedDateTime type, which accounts for time zones and daylight saving time changes, providing accurate representation of exact moments in time with time zone rules.

The Instant type represents an exact time point on the timeline with nanosecond precision, without any calendar or time zone information.

Temporal objects are immutable to prevent unwanted modifications by external code, enhancing reliability and predictability in date and time operations.

Yes, Temporal is designed to support non-Gregorian calendars, allowing developers to accurately handle dates in various cultural and regional contexts.

Temporal provides arithmetic methods such as add, subtract, since, and until, allowing precise duration calculations and adjustments across different types of date and time representations.

Developers can explore the Temporal API through its documentation, and once it graduates, it will be available on MDN alongside other JavaScript built-ins.

The Temporal proposal adds modern date and time handling to JavaScript, providing a built-in library for dates and times similar to those in other programming languages.

Temporal aims to address the deficiencies of the existing Date object in JavaScript, such as unwieldy APIs and mutability issues, by providing a more robust and strongly typed system for date and time management.

Temporal provides a strongly typed and immutable approach to date and time management, with support for non-Gregorian calendars and better integration with JavaScript's internationalization facilities.

 Philip Chimento
Philip Chimento
22 min
21 Nov, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
I'll speak today about the temporal proposal, which adds modern date and time handling to JavaScript. Temporal is an API that'll be available in browsers soon and will add a built-in library for dates and times, avoiding the need for external libraries like Moment. It offers strong typing with different types for different data, such as calendar dates with or without time. Temporal objects are immutable and designed to work with JavaScript's internationalization facilities. It addresses deficiencies in the global date object and introduces types like instant and plain types for accurate representation of time and dates across time zones. With the old date, representing a date without a time can be problematic, especially in time zones where midnight is skipped due to daylight saving time. Temporal introduces types like plain date, plain time, plain year month, plain month day, and zone date time to accurately represent different scenarios. Additionally, there is a type called duration for arithmetic operations and unit conversion. Now that I've introduced you to the cast of characters in temporal, it's time to show how to accomplish a programming task. We'll start with an easy task: getting the current time as a timestamp in milliseconds using the instant type. To convert between Temporal types, you can either drop or add information. The two zone date time method is used for conversion and requires adding a time zone and a time. Although Temporal objects are immutable, you can create new objects with replaced components using the with method. Migrating from the old date object to Temporal offers a more reliable solution and avoids potential bugs. Check out the documentation for more details and enjoy using Temporal in your codebase!
Video transcription and chapters available for users with access.

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

Standardizing Signals in TC39
JSNation US 2024JSNation US 2024
29 min
Standardizing Signals in TC39
I'll be talking about standardizing signals in TC39. Immediate mode wipes out the whole screen and writes a new one, while retained mode changes only what is necessary. Signals represent a cell of data that can change over time. The correct solution is to topologically sort dependencies and evaluate them in a coherent order. Standard signals allow for code sharing across frameworks and the creation of reusable infrastructure. The signal API is designed to be wrapped by different frameworks. Standards can achieve more portability and reduce the need to lock into specific ecosystems. The API includes a watcher, a set of signals being observed, with a synchronous callback made when the first member becomes potentially dirty. The speaker discusses how signals relate to state management libraries in React and mentions the potential for signals to become a web standard in the future.
Future Features of JS?!
JSNation 2022JSNation 2022
28 min
Future Features of JS?!
Top Content
Welcome to the future features of JavaScript, including proposals for array operations, throw expressions, records and TPUs, pipeline operators, and more. The talk covers the introduction of type assertions for imported files, non-mutating array operations, and the simplification of error handling. It also explores the concept of immutability with records and TPUs, and the use of the pipeline operator to simplify code. Other proposals include Map.implace, IteratorHelper, slice notation, type annotations, Array UNIQBY, number ranges, and the Function 1 proposal.
ES?.next()
JSNation Live 2021JSNation Live 2021
31 min
ES?.next()
The Talk discusses various proposals for the next version of ECMAScript (ES Next) and the TC39 process. It covers features such as binding syntax, shorthand property assignments, pattern matching, async match, operator overloading, and more. These proposals aim to simplify code, make it more readable, and introduce new functionalities. The Talk also addresses questions about the committee's decision-making process and the experience of being part of the TC39 committee.
ESNext: Proposals To Look Forward To
JSNation Live 2020JSNation Live 2020
9 min
ESNext: Proposals To Look Forward To
ES Next proposal stages include straw person, proposal, draft, candidate, and finished. Optional chaining and null coalescing operators are solutions for handling undefined and null values. Logical assignment and null coalescing operators are seeking advancement to stage four. Decimal type is introduced to address floating point math issues. Cancellation API and abort control are solutions for canceling promise execution. Pattern matching allows matching the shape of a vector and performing actions based on it.