Skip to content

Commit f2a7571

Browse files
Temporal (#1895)
1 parent f5d0cb0 commit f2a7571

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

examples/scripts/temporal.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22
* @title Temporal API
33
* @difficulty beginner
44
* @tags cli
5-
* @run <url>
5+
* @run --unstable-temporal <url>
66
* @resource {https://docs.deno.com/api/web/~/Temporal} Temporal API reference documentation
77
* @resource {https://tc39.es/proposal-temporal/docs} Temporal API proposal documentation
88
* @group Unstable APIs
99
*/
1010

1111
// Get the current date
12-
const date = Temporal.Now.plainDateISO(); // 2025-01-31
12+
const date = Temporal.Now.plainDateISO();
1313

14-
// Return the date in ISO 8601 date format
15-
const dateAsString = date.toString(); // "2025-01-31"
14+
// Return the date in ISO 8601 date format (eg "2025-01-31")
15+
const dateAsString = date.toString();
1616
console.log(`Temporal date as string: ${dateAsString}`);
1717

18-
// Get current date and time in ISO 8601 format
19-
const plainDateTimeIsoString = Temporal.Now.plainDateTimeISO().toString(); // "2025-01-31T10:51:40.269979904"
18+
// Get current date and time in ISO 8601 format (eg "2025-01-31T10:51:40.269979904")
19+
const plainDateTimeIsoString = Temporal.Now.plainDateTimeISO().toString();
2020
console.log(`Temporal plainDateTimeISO as string: ${plainDateTimeIsoString}`);
2121

22-
// Get Unix timestamp
23-
const timeStamp = Temporal.Now.instant(); // 2025-01-31T18:51:59.093355008Z
22+
// Get Unix timestamp (eg 2025-01-31T18:51:59.093355008Z)
23+
const timeStamp = Temporal.Now.instant();
2424
console.log(`Temporal timestamp as string: ${timeStamp}`);
2525

26-
// Return timestamp in milliseconds
27-
const epochMilliseconds = timeStamp.epochMilliseconds; // 1738349519093
26+
// Return timestamp in milliseconds (eg 1738349519093)
27+
const epochMilliseconds = timeStamp.epochMilliseconds;
2828
console.log(`Temporal timestamp epoch milliseconds: ${epochMilliseconds}`);
2929

30-
// Get date and time in ISO 8601 format from milliseconds
31-
const futureTime = Temporal.Instant.fromEpochMilliseconds(1851222399924); // 2028-08-30T04:26:39.924Z
30+
// Get date and time in ISO 8601 format from milliseconds (eg "2025-01-31T18:51:59.093Z")
31+
const futureTime = Temporal.Instant.fromEpochMilliseconds(1851222399924);
3232
console.log(`Temporal future time: ${futureTime}`);
3333

3434
// Measure difference in hours from now.
3535
const now = Temporal.Now.instant();
36-
const differenceInHours = now.until(futureTime, { smallestUnit: "hour" }); // PT31600H
36+
const differenceInHours = now.until(futureTime, { smallestUnit: "hour" });
3737
console.log(`Temporal difference in hours: ${differenceInHours}`);

runtime/reference/cli/unstable_flags.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,43 @@ deno run --unstable your_script.ts
241241

242242
It is recommended that you use the granular unstable flags instead of this, the
243243
`--unstable` flag is now deprecated and will be removed in Deno 2.
244+
245+
## `--unstable-temporal`
246+
247+
Enable the experimental [Temporal API](https://tc39.es/proposal-temporal/docs/)
248+
in the global scope. The Temporal API is a modern date and time API that is
249+
being developed as a replacement for the legacy `Date` object in JavaScript. It
250+
provides better support for time zones, calendars, and more precise date/time
251+
calculations.
252+
253+
To use the Temporal API in your Deno program, run it with the flag:
254+
255+
```sh
256+
deno run --unstable-temporal your_script.ts
257+
```
258+
259+
Or configure it in your `deno.json`:
260+
261+
```json title="deno.json"
262+
{
263+
"unstable": ["temporal"]
264+
}
265+
```
266+
267+
```ts title="example.ts"
268+
// Example using Temporal API
269+
const now = Temporal.Now.plainDateTimeISO();
270+
console.log(`Current date and time: ${now}`);
271+
272+
const date = Temporal.PlainDate.from("2025-07-10");
273+
const nextWeek = date.add({ days: 7 });
274+
console.log(`Next week: ${nextWeek}`);
275+
276+
// Working with time zones
277+
const zonedDateTime = Temporal.Now.zonedDateTimeISO("America/New_York");
278+
console.log(`Time in New York: ${zonedDateTime}`);
279+
```
280+
281+
Note that the Temporal API is still experimental and the specification may
282+
change before it becomes stable. Use this flag only for testing and
283+
experimentation.

0 commit comments

Comments
 (0)