Порівняння двох дат

Lua:
Порівняння двох дат

How to: (Як це зробити:)

-- Load the os.date table
local date1 = os.date("*t", os.time({year=2023, month=3, day=15}))
local date2 = os.date("*t", os.time({year=2023, month=4, day=15}))

-- Compare dates
if os.time(date1) < os.time(date2) then
  print("Date1 is earlier than Date2")
else
  print("Date2 is earlier than Date1")
end

Sample Output:

Date1 is earlier than Date2

Deep Dive (Поглиблений Розбір:)

Lua doesn’t have built-in date comparison functions. Instead, you compare timestamps.

Historically, timestamps represent seconds since the epoch (January 1, 1970). They make date comparison straightforward with simple arithmetic. Lua’s os.time() turns a date table into a timestamp.

Utilities like os.date("*t", ...) build date tables. Alternatives in Lua include custom date libraries or parsing date strings manually. But be wary of timezones and daylight saving time.

Using native Lua functions ensures compatibility and simplicity. Timestamps are integers, so they’re also efficient to compare.

See Also (Дивіться також):