Aller au contenu principal

DateTime():diff()

Calculates the difference between the current DateTime object and another DateTime object.

Signature

diff(other: DateTime): number
diff(other: DateTime, unit: string): number

Parameters

ParameterTypeDescription
otherDateTimeThe other DateTime object to compare against.
unit (Optional)stringThe unit of time for the difference. If not provided, the difference is returned in milliseconds. Supported units include milliseconds, seconds, minutes, hours, days

Returns

  • Type: number
  • The difference in milliseconds, or in the specified unit if provided.

Examples

local date1 = DateTime(2024, 1, 10, 10, 0, 0)
local date2 = DateTime(2024, 1, 10, 9, 0, 0)

local diffMillis = date1:diff(date2) -- Difference in milliseconds
toast("Difference in milliseconds: " .. diffMillis)

local diffHours = date1:diff(date2, "hours") -- Difference in hours
toast("Difference in hours: " .. diffHours)

local diffDays = date1:diff(date2, "days") -- Difference in days
toast("Difference in days: " .. diffDays)