[英]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.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.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).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.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"
."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"
."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.