跳到主要内容

DateTime():startOf()

将 DateTime 对象设置为指定时间单位的起始时刻(例如当天开始、当月开始)。

签名​

startOf(unit: string): DateTime

参数​

参数类型描述
unitstring要设置起始时刻的时间单位。支持的单位包括:year、month、week、day、hour、minute、second

返回值​

  • 类型:DateTime
  • 当前的 DateTime 实例,可用于方法链式调用。

示例​

local now = DateTime()
-- 设置为当天开始
now:startOf("day")
toast("Start of day: " .. now:format("yyyy-MM-dd HH:mm:ss.SSS"))

-- 设置为当月开始
now = DateTime() -- 重置
now:startOf("month")
toast("Start of month: " .. now:format("yyyy-MM-dd HH:mm:ss.SSS"))

-- 设置为当年开始
now = DateTime() -- 重置
now:startOf("year")
toast("Start of year: " .. now:format("yyyy-MM-dd HH:mm:ss.SSS"))