跳到主要内容

DateTime():tz()

DateTime 对象的时区设置为指定的时区 ID。

签名

tz(timeZoneId: string): DateTime

参数

参数类型描述
timeZoneIdstring要设置的时区 ID(例如 "America/New_York"、"Europe/London")。可用的时区 ID 列表可在此处查看。

返回值

  • 类型:DateTime
  • 当前的 DateTime 实例,其时区已设置为指定 ID,可用于方法链式调用。

示例

local now = DateTime()
toast("Local time: " .. now:format("yyyy-MM-dd HH:mm:ss Z"))

now:tz("America/New_York")
toast("New York time: " .. now:format("yyyy-MM-dd HH:mm:ss Z"))

now:tz("Europe/London")
toast("London time: " .. now:format("yyyy-MM-dd HH:mm:ss Z"))