跳至正文
rdlabo.dev文档

API

以下导出项均来自 @rdlabo/workers-timezone。省略 timeZone? 时,使用初始化时配置的默认时区,尚未初始化时使用 Asia/Tokyo。错误处理和夏令时(DST)行为见时区。

配置

function initializeTimezone

initializeTimezone(config: TimezoneConfig): Readonly<TimezoneConfig> 会一次性设置默认时区配置。

function getTimezoneConfig

getTimezoneConfig(): Readonly<TimezoneConfig> 返回当前激活的时区配置。

转换

function toLocalDate

toLocalDate(instant: Date, timeZone?: TimeZone): BusinessDate 返回 YYYY-MM-DD 格式的时间。

function toLocalDateTime

toLocalDateTime(instant: Date, timeZone?: TimeZone): BusinessDateTime 返回 YYYY-MM-DD HH:mm:ss 格式的时间。

function localDateTimeToInstant

localDateTimeToInstant(date: BusinessDate, time: string, timeZone?: TimeZone): Date 用于将本地时间转换为 Instant 时间。时间参数可以接受H:mm或HH:mm格式,并可选提供秒数。

function startOfDay

startOfDay(date: BusinessDate, timeZone?: TimeZone): Date 返回一天中的第一个时刻。

function endOfDay

endOfDay(date: BusinessDate, timeZone?: TimeZone): Date 返回其最终的整秒数。

function addDays

addDays(date: BusinessDate, days: number): BusinessDate 添加整数个日历天数。

其他日历辅助函数

today(reference?: Date, timeZone?: TimeZone) 返回日历日期。normalizeBusinessDate(value: string | Date | null | undefined, timeZone?: TimeZone) 返回日历日期或 null。传入具体时刻时,建议使用带显式偏移量的 ISO 字符串。formatBusinessDateTime(instant, pattern?, timeZone?) 支持 YYYY、MM、DD、hh、mm、ss 和 S 这些格式标记;DEFAULT_BUSINESS_DATETIME_PATTERN 为 YYYY-MM-DDThh:mm:ss。parseBusinessDateTime(value: BusinessDateTime, timeZone?: TimeZone): Date 解析本地 YYYY-MM-DD HH:mm:ss 值(也接受 T 分隔符)。ageOnBusinessDate(birthDate: BusinessDate, asOfDate?: BusinessDate): number 计算周岁;参照日期默认为 today()。

类型与常量

TIME_ZONES 包含常见的 IANA 标识符。TimeZone 也接受其他支持的 IANA 字符串。TimezoneConfig 包含 timeZone 字段。BusinessDate 和 BusinessDateTime 是字符串别名,并非运行时验证器。BusinessTimeZone 和 BusinessTimeConfig 是兼容类型别名。BUSINESS_TIMEZONE 是旧的东京时区描述,并非当前配置的时区。

兼容性导出函数:toBusinessDate、toBusinessDateTime、businessDateTimeInstant、startOfBusinessDay、endOfBusinessDay 和 addBusinessDays 分别对应上述函数。BUSINESS_TIME_ZONES、initializeBusinessTime 和 getBusinessTimeConfig 分别对应时区常数和配置函数。