lua比较两个时间点,返回相差几天

-- 上一个时间
    local lastYear = os.date("%Y", 1490889600)
    local lastMonth = os.date("%m", 1490889600)
    local lastDay = os.date("%d", 1490889600)
    print("lastYear==="..lastYear)
    print("lastMonth==="..lastMonth)
    print("lastDay==="..lastDay)

    -- 当前时间
    local curYear = os.date("%Y", 1490976000)
    local curMonth = os.date("%m", 1490976000)
    local curDay = os.date("%d", 1490976000)
    print("curYear==="..curYear)
    print("curMonth==="..curMonth)
    print("curDay==="..curDay)

    --[[比较两个时间,返回相差多少时间]]  
    function timediff(long_time,short_time)  
        local n_short_time,n_long_time,carry,diff = os.date('*t',short_time),os.date('*t',long_time),false,{}  
        local colMax = {60,60,24,os.date('*t',os.time{year=n_short_time.year,month=n_short_time.month+1,day=0}).day,12,0}  
        n_long_time.hour = n_long_time.hour - (n_long_time.isdst and 1 or 0) + (n_short_time.isdst and 1 or 0) -- handle dst  
        for i,v in ipairs({'sec','min','hour','day','month','year'}) do  
            diff[v] = n_long_time[v] - n_short_time[v] + (carry and -1 or 0)  
            carry = diff[v] < 0  
            if carry then  
                diff[v] = diff[v] + colMax[i]
            end  
        end  
        return diff  
    end  

      
    local n_long_time = os.date(os.time{year=curYear,month=curMonth,day=curDay,hour=0,min=0,sec=0});
    local n_short_time = os.date(os.time{year=lastYear,month=lastMonth,day=lastDay,hour=0,min=0,sec=0});
      
      
    local t_time = timediff(n_long_time,n_short_time);  
    local time_txt = string.format("%04d", t_time.year).."年"..string.format("%02d", t_time.month).."月"..string.format("%02d", t_time.day).."日   "..string.format("%02d", t_time.hour)..":"..string.format("%02d", t_time.min)..":"..string.format("%02d", t_time.sec);  
    print(time_txt); 


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值