简体   繁体   English

在 JavaScript 中,用于解析特定字符串的正确“时间”类型是什么?

[英]In JavaScript, what is the correct `Temporal` type to use for parsing a particular string?

ECMAScript's new Temporal proposal defines (finally!) a modern API for handling dates and times in JS, including timezone-safe arithmetic and non-Gregorian calendars. ECMAScript 的新Temporal提案定义(终于!)一个现代 API,用于处理 JS 中的日期和时间,包括时区安全的算术和非公历。

I know I can use ISO 8601 or RFC 3339 strings with Temporal .我知道我可以将 ISO 8601 或 RFC 3339 字符串与Temporal一起使用。 If I have string data that I want to parse into a JS date/time object, which Temporal type should I use?如果我有要解析为 JS 日期/时间对象的字符串数据,我应该使用哪种 Temporal 类型?

To determine the Temporal class that should be used to parse a string, it's important to pick the type whose data model matches the data in the string.要确定应该用于解析字符串的Temporal类,选择其数据模型与字符串中的数据匹配的类型很重要。

  • Temporal.Instant represents an exact time. Temporal.Instant代表一个准确的时间。 Its data model is the number of integer nanoseconds since January 1, 1970 UTC.它的数据模型是自 1970 年 1 月 1 日 UTC 以来的整数纳秒数。 This type is unaware of time zones and does not provide a way to access calendar/clock units like month or hour.这种类型不知道时区,并且不提供访问日历/时钟单位(如月或小时)的方法。 It's just a timestamp.这只是一个时间戳。
  • Temporal.ZonedDateTime represents an exact time from the perspective of a specific time zone. Temporal.ZonedDateTime表示从特定时区的角度来看的准确时间。 Its data model is a timestamp (like Temporal.Instant ), a time zone (usually an IANA zone), and a calendar.它的数据模型是时间戳(如Temporal.Instant )、时区(通常是 IANA 区域)和日历。 The time zone is needed because this Temporal type (and only this type) allows DST-safe creation of derived values.需要时区,因为这种Temporal类型(并且只有这种类型)允许 DST 安全地创建派生值。 When you add 1 day to a Temporal.ZonedDateTime instance, the exact time will usually be 24 hours later but it may be 23 or 25 if the addition crossed a DST transition.当您将 1 天添加到Temporal.ZonedDateTime实例时,确切时间通常会晚 24 小时,但如果添加跨越 DST 转换,则可能是 23 或 25。
  • Temporal.PlainDate represents a timezone-less date: a local date without a time and without any reference to the time zone. Temporal.PlainDate表示无时区日期:没有时间且不参考时区的本地日期。 Its data model is a year/month/day and a calendar.它的数据模型是年/月/日和日历。
  • Temporal.PlainMonthDay represents a birthday, anniversary, or other recurring event. Temporal.PlainMonthDay表示生日、周年纪念日或其他重复事件。 Its data model is a month, day, and calendar.它的数据模型是月、日和日历。
  • Temporal.PlainYearMonth represents a month in a specific year, eg January 2022. Its data model is a year, month, and calendar. Temporal.PlainYearMonth表示特定年份中的一个月,例如 2022 年 1 月。它的数据模型是年、月和日历。
  • Temporal.PlainTime represents a timezone-less time of day: a local time without reference to date, time zone, or calendar. Temporal.PlainTime表示一天中无时区的时间:不参考日期、时区或日历的本地时间。 Its data model is an hour/minute/second, with seconds resolution down to 9 decimal digits (nanoseconds).它的数据模型是小时/分钟/秒,秒分辨率低至 9 位十进制数字(纳秒)。
  • Temporal.PlainDateTime represents a local date and time without any reference to the time zone. Temporal.PlainDateTime表示本地日期和时间,不参考时区。 Its data model is year/month/day/hour/minute/second and a calendar.它的数据模型是年/月/日/时/分/秒和一个日历。 This type is rarely used, because the types above cover most use cases.这种类型很少使用,因为上面的类型涵盖了大多数用例。 If you only care about the exact timestamp and don't care about time zones, then use Temporal.Instant .如果您只关心确切的时间戳而不关心时区,请使用Temporal.Instant If you only care about the local date, then use Temporal.PlainDate .如果您只关心本地日期,请使用Temporal.PlainDate If you care about the exact time and the time zone, then use Temporal.ZonedDateTime .如果您关心确切的时间和时区,请使用Temporal.ZonedDateTime Other than the few use cases detailed in the Temporal.PlainDateTime documentation, most of the time it's better to use a different type.除了Temporal.PlainDateTime文档中详述的少数用例之外,大多数情况下最好使用不同的类型。
  • Temporal.Duration represents a period of time. Temporal.Duration表示一段时间。 Its data model is a number of years, months, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.它的数据模型是若干年、月、日、小时、分钟、秒、毫秒、微秒和纳秒。
  • Temporal.TimeZone represents an IANA time zone like Asia/Tokyo or (rarely) an offset time zone like +06:00 . Temporal.TimeZone代表 IANA 时区,例如Asia/Tokyo或(很少)偏移时区,例如+06:00 Its data model is the canonical ID of the time zone, eg "Asia/Tokyo" or "+06:00" .其数据模型是时区的规范 ID,例如"Asia/Tokyo""+06:00"
  • Temporal.Calendar represents a calendar like Hebrew, Chinese, or the default ISO 8601 calendar. Temporal.Calendar表示类似于希伯来语、中文或默认 ISO 8601 日历的日历。 Its data model is the ID of the calendar, eg "iso8601" or "hebrew" .它的数据模型是日历的ID,例如"iso8601""hebrew"

Note that localized string formats like '12/25/2022' are not parseable by Temporal.请注意,像'12/25/2022'这样的本地化字符串格式不能被 Temporal 解析。 Only unambiguous, computer-readable formats can be parsed by Temporal, like '2022-25-12' . Temporal 只能解析明确的计算机可读格式,例如'2022-25-12'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM