lua 时间类处理

1:-- 小时:分钟:秒

function SecondToStrFormat( time )
    local hour = math.floor(time/3600); --向下取整
    local minute = math.fmod(math.floor(time/60), 60)     --math.fmod( 取模就是求余的意思,比如3%2=1这样子。
    local second = math.fmod(time, 60)
    return string.format("%02d:%02d:%02d", hour, minute, second), hour, minute, second

end

2:-- 年,月,日
function SecondToStrFormat3( time )
    local timeTab = os.date('*t', time)
    local str = ''
    str = timeTab.year..'年'..timeTab.month..'月'..timeTab.day..'日'
    return str

end

3:-- 当时间小于100分钟
function SecondToStrFormat2(time)
if time >= 100 * 60 then
logError("Too Much Time,Please Use SecondToStrFormat Method")
return
end
local minute = math.floor(time/60)
local second = math.fmod(time,60)
return string.format("%02d:%02d",minute,second),minute, second;

end

4:每周

 local nowTime = os.time()

 local nowTimeTab = os.date("*t",nowTime)

 local nowWake = nowTimeTab.wday  1周日  2-7是周一到周六

5:当天的零点

    nowTimeTab.hour = 0
    nowTimeTab.min =0
    nowTimeTab.sec =0

    local timeZero = os.time(nowTimeTab)

6: "day"   = 1      --日
     "hour"  = 8     --小时
     "isdst" =false     --是否夏令时 如果有夏令时,则需要加上3600s
     "min"   = 0     --分钟
     "month" = 1     --月
     "sec"   = 0     --秒
     "wday"  = 5     --星期5
     "yday"  = 1     --当年已过天数

     "year"  = 1970  --年

print("===============os.date()==",os.date("%Y-%m-%d%H:%M:%S",1479892620))

输出:===============os.date()==   2016-11-23﹎17:17:00

7:获得当月最后一天

通过os.time()获取当前月份的下一个月减去1天(即当月最后一天)的时间

os.date("%d",os.time({year=os.date("%Y"),month=os.date("%m")+1,day=0}))

8:计算当前之后每周的时间显示

--初始化数据 计算当前时间
function InitData()
    local nowTime = SystemTime()
    local nowTimeTab = os.date("*t",nowTime)
    local nowWake = nowTimeTab.wday   --1周日,  2-7周六
    local timeZero = GetNowDayZeroTime(nowTime) --当天0点时间
    --算出当前周期 周一的零点时间 每次加7天时间
    local tempTime =0
    if nowWake == 1 then
        tempTime = timeZero - 24*3600
    else
        tempTime = timeZero - 24*3600*(nowWake-2)

    end


    self._dataTime = {}
    --总共三周
    for i=1,3 do
        local time1 = tempTime+24*3600*7*(i-1) +10
        local time2 = tempTime+24*3600*7*i -10
        table.insert(self._dataTime,{starTime= time1,endTime = time2})
    end
end


猜你喜欢

转载自blog.csdn.net/qq_30585525/article/details/79502263