以前寫過一篇關於把秒轉換成指定的日期格式 Lua date formathtml
接到一個需求,須要從配置文件中讀取活動顯示時間段:startDate ~ endDate(格式爲:yyyy-mm-dd HH:MM:SS),而後與服務器返回的時間進行比較,若是在該時間段內則顯示該活動,如何實現呢?服務器
首先咱們藉助「split」函數來得到相應的年、月、日、時、分、秒,而後將其轉換爲秒再比較app
-- Compatibility: Lua-5.1
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
---- 經過日期獲取秒 yyyy-MM-dd HH:mm:ss
function GetTimeByDate(r)
local a = split(r, " ")
local b = split(a[1], "-")
local c = split(a[2], ":")
local t = os.time({year=b[1],month=b[2],day=b[3], hour=c[1], min=c[2], sec=c[3]})
return t
end