Some functions of lua date

--Get the day of the week according to the date 
function getWeekNum(strDate)
     local ymd = Split(strDate, " - " )
    t = os.time({year=tostring(ymd[1]),month=tostring(ymd[2]),day=tostring(ymd[3])})
    local weekNum = os.date("*t",t).wday  -1
    if weekNum == 0 then
        weekNum = 7
    end
    return weekNum
end

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

 

--Get the next days according to a date, the dayChange parameter is the next days 
function getNextDay(strDate,dayChange)
     local  _startDate = Split(startDate, " - " )    
     local time= os.time ({year= tostring ( _startDate [ 1 ] ), month= tostring ( _startDate [ 2 ]), day= tostring ( _startDate [ 3 ])})+dayChange* 86400  -- day 86400 seconds 
    return ( os.date ( ' %Y ' ,time).."-"..os.date('%m',time).."-"..os.date('%d',time))    
end

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

 

--Calculate the days in a time period 
function getDays(startDate, endDate)
     local startDateTab = Split(startDate, " - " )
     local endDateTab = Split(endDate, " - " )    
    numDay1 = os.time({year=tostring(startDateTab[1]), month=tostring(startDateTab[2]), day=tostring(startDateTab[3])})
    numDay2 = os.time({year=tostring(endDateTab[1]), month=tostring(endDateTab[2]), day=tostring(endDateTab[3])})    
    return (numDay1-numDay2)/(3600*24)+1;
end

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

 

-- Traverse whether a value is in the array 
function IsInTable(value, tbl)
         for k,v in  ipairs (tbl) do 
                if v == value then 
                        return  true ;
                 end 
        end 
        return  false ;
 end

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695952&siteId=291194637