Escribe para buscar…

Temporal

Temporal is the modern replacement for the problematic JavaScript Date object.

Esta página todavía no se ha traducido — se muestra en su idioma original:English

JavaScript’s Date object has been a source of frustration for decades. It’s mutable, doesn’t support non-Gregorian calendars well, and handling time zones is notoriously difficult.

The Temporal API is the modern solution, now stable in Deno since v2.7.

Why Temporal?

As Mat Marquis explains in his post, Date is essentially “broken” for modern web development:

  1. Mutability: Calling a method like setHours() on a Date object changes the original object, leading to subtle bugs.
  2. Parsing: Date.parse() is notoriously inconsistent across different browsers.
  3. Time Zones: Date only supports the user’s local time and UTC.

Temporal solves these problems by being immutable and providing specialized types for different use cases.

Getting Started

In Deno, Temporal is available globally. You don’t need to import anything.

Current Date and Time

The Temporal.Now object provides methods to get the current time in various formats:

ts
// Current date (e.g., 2026-04-07)
const today = Temporal.Now.plainDateISO();

// Current date and time (e.g., 2026-04-07T12:14:30)
const now = Temporal.Now.plainDateTimeISO();

// Exact point in time (UTC)
const instant = Temporal.Now.instant();

Core Types

Temporal introduces several types to represent time more accurately:

PlainDate

Represents a date with no time or time zone (e.g., a birthday or a calendar event).

ts
const birthday = Temporal.PlainDate.from("1995-12-07");
console.log(birthday.year); // 1995

PlainTime

Represents a time of day with no date or time zone (e.g., “the store opens at 9:00”).

ts
const openingTime = Temporal.PlainTime.from("09:00:00");

ZonedDateTime

A complete date and time in a specific time zone. This is what you should use for most “real-world” events.

ts
const meeting = Temporal.ZonedDateTime.from(
  "2026-03-15T14:30[America/New_York]"
);

// Convert to another time zone easily
const inTokyo = meeting.withTimeZone("Asia/Tokyo");

Estás leyendo una vista previa.

Inicia sesión para leer el artículo completo. Cualquier cuenta abre 4 artículos gratuitos al mes; el alumnado y el profesorado leen las páginas de su curso sin límite.

Iniciar sesión