如何将日期格式转换为UNIX时间戳?



我正在尝试转换日期(假设是5/08/2022),然后我想检查它是否超过当前日期。

因此,例如:我正在制作一个系统,它将检查过期日期是否超过当前日期。如果是,该布尔值将为真。

任何帮助都将非常感激,因为我对Lua编程相当陌生。

-- Check Hunting License --
QBCore.Functions.CreateCallback('hidden-hunting:server:getLicenseStatus', function(source, cb)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local licenseTable = Player.PlayerData.metadata["licences"]
local licenseItem = Player.Functions.GetItemByName("huntinglicense")
local isExpire = false  

local timeToConvert = licenseTable.huntingexp

print("Time from Huntingexp: "..timeToConvert)

local m, d, y = string.match(timeToConvert, "([^/]+)/([^/]+)/([^/]+)")
local convertedTimestamp = os.time({month = m, day = d, year = y})

print("Converted from String: "..convertedTimestamp)
print("Current: "..os.time())

local daysfrom = os.difftime(os.time(), convertedTimestamp) / (24 * 60 * 60) -- seconds in a day
local wholedays = math.floor(daysfrom)

print("Days? "..wholedays)
local current = os.time()
if current > convertedTimestamp then
print("AH?")
isExpired = true
end

cb(licenseTable.hunting, licenseItem, isExpired)
end)

我看到你在用QBCore for FiveM。

local currentTime = os.time() * 1000
local m, d, y = string.match("05/08/2022", "([^/]+)/([^/]+)/([^/]+)")
local convertedTimestamp = os.time({month = m, day = d, year = y}) * 1000
if (currentTime > convertedTimestamp) then
print("Expired")
else
print("Not expired")
end

以上代码将当前日期时间和许可证日期时间转换为UNIX(因为os.time()秒返回当前时间)而不是毫秒)并进行比较。

最新更新