22
33Temporal 是一个表示日期时间的全新 API,对目前的 Date API 的诸多问题进行修正。
44
5+ 它有几个核心概念。
6+
7+ - 当前时间:表示此时此刻的时间,位于 Temporal.now 对象。
8+ - 时点(instant),表示历史上某个唯一时间,其中 Temporal.Instant 对象表示时间戳,Temporal.ZonedDateTime 表示带有时区的日期时间。
9+ - 时钟时间(wall-clock times),表示本地时间,包含以下几个对象,不涉及时区。
10+ - Temporal.PlainDateTime:完整的日期和时间。
11+ - Temporal.PlainDate:仅限于日期。
12+ - Temporal.PlainYearMonth:仅限于年月。
13+ - Temporal.PlainMonthDay:仅限于月和日。
14+ - Temporal.PlainTime:不包含日期的时间。
15+ - 持续时间(durations),表示两个时间点之间的差异,位于 Temporal.Duration 对象。
16+
517## Temporal.Now
618
719` Temporal.Now ` 表示当前系统的准确时间。
@@ -17,6 +29,12 @@ Temporal 是一个表示日期时间的全新 API,对目前的 Date API 的诸
1729// 返回 UTC 的当前时间
1830Temporal .Now .instant ().toString ()
1931
32+ // 系统时区的当前时间
33+ Temporal .Now .plainDateTimeISO () // 2025-01-22T11:46:36.144
34+
35+ // 当前时间对应 America/New_York 时区的时间
36+ Temporal .Now .plainDateTimeISO (" America/New_York" ) // 2025-01-22T05:47:02.555
37+
2038// 返回某个时区的当前日期时间
2139Temporal .Now .zonedDateTimeISO (' Asia/Shanghai' ).toString ()
2240
@@ -34,14 +52,25 @@ const now = Temporal.Now.zonedDateTimeISO('America/New_York');
3452console .log (now .toString ());
3553```
3654
55+ 下面的例子是获取当前时间对应的农历年。
56+
57+ ``` javascript
58+ const currentYear = Temporal .Now .plainDateISO ().withCalendar (" chinese" ).year ;
59+ ```
60+
3761## Temporal.Instant
3862
39- ` Temporal.Instant ` 表示某个固定的时间 。
63+ ` Temporal.Instant ` 表示某个固定的时点 。
4064
4165``` javascript
4266const instant = Temporal .Instant .from (' 1969-07-20T20:17Z' );
4367instant .toString (); // => '1969-07-20T20:17:00Z'
4468instant .epochMilliseconds ; // => -14182980000
69+
70+ // 某个 Unix 时间戳对应的时点
71+ const launch = Temporal .Instant .fromEpochMilliseconds (1851222399924 );
72+ const now = Temporal .Now .instant ();
73+ const duration = now .until (launch, { smallestUnit: " hour" });
4574```
4675
4776## Temporal.ZonedDateTime
@@ -211,6 +240,27 @@ birthdayIn2030.toString() // 2030-12-15
211240birthdayIn2030 .dayOfWeek // 7
212241```
213242
243+ 下面是农历一月一日(大年初一)的例子。
244+
245+ ``` javascript
246+ const chineseNewYear = Temporal .PlainMonthDay .from ({
247+ monthCode: " M01" ,
248+ day: 1 ,
249+ calendar: " chinese" ,
250+ });
251+
252+ const currentYear = Temporal .Now .plainDateISO ().withCalendar (" chinese" ).year ;
253+
254+ // 获取下一个春节
255+ let nextCNY = chineseNewYear .toPlainDate ({ year: currentYear });
256+ // 如果 nextCNY 早于当前时间,则向后移动一年
257+ if (Temporal .PlainDate .compare (nextCNY, Temporal .Now .plainDateISO ()) <= 0 ) {
258+ nextCNY = nextCNY .add ({ years: 1 });
259+ }
260+
261+ nextCNY .withCalendar (" iso8601" ).toLocaleString () // 1/29/2025
262+ ```
263+
214264## Temporal.Duration
215265
216266` Temporal.Duration ` 表示时长。
@@ -247,8 +297,26 @@ date.monthsInYear; // => 12
247297date .daysInYear ; // => 365
248298```
249299
300+ ## Temporal.Duration
301+
302+ Temporal.Duration 表示一个持续的时间对象。
303+
304+ ``` javascript
305+ const durations = [
306+ Temporal .Duration .from ({ hours: 1 }),
307+ Temporal .Duration .from ({ hours: 2 }),
308+ Temporal .Duration .from ({ hours: 1 , minutes: 30 }),
309+ Temporal .Duration .from ({ hours: 1 , minutes: 45 }),
310+ ];
311+
312+ durations .sort (Temporal .Duration .compare );
313+ console .log (durations .map ((d ) => d .toString ()));
314+ // [ 'PT1H', 'PT1H30M', 'PT1H45M', 'PT2H' ]
315+ ````
316+
250317## 参考链接
251318
252319- [Temporal documentation](https: // tc39.es/proposal-temporal/docs/)
253320- [JS Dates Are About to Be Fixed](https: // docs.timetime.in/blog/js-dates-finally-fixed/)
321+ - [JavaScript Temporal is coming](https: // developer.mozilla.org/en-US/blog/javascript-temporal-is-coming/)
254322
0 commit comments