DateTime():diff()
计算当前 DateTime 对象与另一个 DateTime 对象之间的差值。
签名
diff(other: DateTime): number
diff(other: DateTime, unit: string): number
参数
| 参数 | 类型 | 描述 |
|---|---|---|
other | DateTime | 用于比较的另一个 DateTime 对象。 |
unit(可选) | string | 差值的时间单位。如未提供,则差值以毫秒返回。支持的单位包括 milliseconds、seconds、minutes、hours、days |
返回值
- 类型:
number - 以毫秒为单位的差值,若提供了单位,则为指定单位的差值。
示例
local date1 = DateTime(2024, 1, 10, 10, 0, 0)
local date2 = DateTime(2024, 1, 10, 9, 0, 0)
local diffMillis = date1:diff(date2) -- 以毫秒为单位的差值
toast("Difference in milliseconds: " .. diffMillis)
local diffHours = date1:diff(date2, "hours") -- 以小时为单位的差值
toast("Difference in hours: " .. diffHours)
local diffDays = date1:diff(date2, "days") -- 以天为单位的差值
toast("Difference in days: " .. diffDays)