|
2 | 2 | * @title Temporal API
|
3 | 3 | * @difficulty beginner
|
4 | 4 | * @tags cli
|
5 |
| - * @run <url> |
| 5 | + * @run --unstable-temporal <url> |
6 | 6 | * @resource {https://docs.deno.com/api/web/~/Temporal} Temporal API reference documentation
|
7 | 7 | * @resource {https://tc39.es/proposal-temporal/docs} Temporal API proposal documentation
|
8 | 8 | * @group Unstable APIs
|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | // Get the current date
|
12 |
| -const date = Temporal.Now.plainDateISO(); // 2025-01-31 |
| 12 | +const date = Temporal.Now.plainDateISO(); |
13 | 13 |
|
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(); |
16 | 16 | console.log(`Temporal date as string: ${dateAsString}`);
|
17 | 17 |
|
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(); |
20 | 20 | console.log(`Temporal plainDateTimeISO as string: ${plainDateTimeIsoString}`);
|
21 | 21 |
|
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(); |
24 | 24 | console.log(`Temporal timestamp as string: ${timeStamp}`);
|
25 | 25 |
|
26 |
| -// Return timestamp in milliseconds |
27 |
| -const epochMilliseconds = timeStamp.epochMilliseconds; // 1738349519093 |
| 26 | +// Return timestamp in milliseconds (eg 1738349519093) |
| 27 | +const epochMilliseconds = timeStamp.epochMilliseconds; |
28 | 28 | console.log(`Temporal timestamp epoch milliseconds: ${epochMilliseconds}`);
|
29 | 29 |
|
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); |
32 | 32 | console.log(`Temporal future time: ${futureTime}`);
|
33 | 33 |
|
34 | 34 | // Measure difference in hours from now.
|
35 | 35 | const now = Temporal.Now.instant();
|
36 |
| -const differenceInHours = now.until(futureTime, { smallestUnit: "hour" }); // PT31600H |
| 36 | +const differenceInHours = now.until(futureTime, { smallestUnit: "hour" }); |
37 | 37 | console.log(`Temporal difference in hours: ${differenceInHours}`);
|
0 commit comments