lua 日期的一些函数

时间:2023-02-07 12:10:59
--根据日期获取星期几
function getWeekNum(strDate)
local ymd = Split(strDate,"-")
t = os.time({year=tostring(ymd[]),month=tostring(ymd[]),day=tostring(ymd[])})
local weekNum = os.date("*t",t).wday -
if weekNum == then
weekNum =
end
return weekNum
end

ngx.say(getWeekNum("2018-04-25"))

--根据一个日期获取下几天,dayChange参数为下几天
function getNextDay(strDate,dayChange)
local _startDate = Split(startDate,"-")
local time=os.time({year=tostring(_startDate[]), month=tostring(_startDate[]), day=tostring(_startDate[])})+dayChange* --一天86400秒
return (os.date('%Y',time).."-"..os.date('%m',time).."-"..os.date('%d',time))
end

ngx.say(getNextDay("2018-04-25", 1))

--计算一个时间段中有几天
function getDays(startDate, endDate)
local startDateTab = Split(startDate,"-")
local endDateTab = Split(endDate,"-")
numDay1 = os.time({year=tostring(startDateTab[]), month=tostring(startDateTab[]), day=tostring(startDateTab[])})
numDay2 = os.time({year=tostring(endDateTab[]), month=tostring(endDateTab[]), day=tostring(endDateTab[])})
return (numDay1-numDay2)/(*)+;
end

ngx.say(getDays("2018-04-25", "2018-04-20"))

--遍历一个值是否在数组中
function IsInTable(value, tbl)
for k,v in ipairs(tbl) do
if v == value then
return true;
end
end
return false;
end
--获取前10分钟
ngx.print(os.date("%Y%m%d%H%M%S",os.time()-))