Lua
Lua
Compute Difference Between Two DateTime Strings
Demonstrates how to compute the difference between two datetime strings.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Imagine we have these strings:
local t1 = "2022-11-14 15:45:38"
local t2 = "2022-11-16 17:23:41"
-- How many minutes are between the two?
-- First, we'd want load each date/time into a Chilkat CkDateTime object.
-- In this case, there are no CkDateTime methods that accept the above format.
-- However.. the CkDateTime's SetFromTimestamp will accept a date in the format "YYYY-MM-DDThh:mm:ssZ"
-- First modify the above dates to the timestamp format.
local sb1 = chilkat.StringBuilder.new()
sb1:append(t1)
local count = sb1:replace(" ", "T")
sb1:append("Z")
local sb2 = chilkat.StringBuilder.new()
sb2:append(t2)
count = sb2:replace(" ", "T")
sb2:append("Z")
-- Load each into a CkDateTime
local dt1 = chilkat.DateTime.new()
dt1:set_from_timestamp(sb1:get_as_string())
-- verify...
print(dt1:get_as_timestamp(false))
local dt2 = chilkat.DateTime.new()
dt2:set_from_timestamp(sb2:get_as_string())
-- verify...
print(dt2:get_as_timestamp(false))
-- Get the difference in seconds
local diffSeconds = dt2:diff_seconds(dt1)
print("Difference in seconds: " .. diffSeconds)
print("Difference in minutes: " .. (math.floor(diffSeconds / 60)))