跳到主要内容

DateTime():add()

DateTime 对象添加指定数量的时间。

签名

add(amount: number, unit: string)

参数

参数类型描述
amountint要添加的数量。
unitString要添加的时间单位。支持的单位包括:yearsyearmonthsmonthweeksweekdaysdayhourshourminutesminutesecondssecondmillisecondsmillisecond

返回值

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

示例

local now = DateTime()
-- 添加 5
now:add(5, "days")
toast("Date after adding 5 days: " .. now:format("yyyy-MM-dd"))

-- 添加 2 个月
now = DateTime() -- 重置
now:add(2, "months")
toast("Date after adding 2 months: " .. now:format("yyyy-MM-dd"))