lua 获取当前时刻到自然周、自然月结束还剩多长时间

获取此刻到当天24:00、本周末24:00、本月最后一天24:00还剩多少时间,用于倒计时​​​​​​​

 

function getWeekNum()
    --当前是星期几
    t = os.time()
    local weekNum = os.date("*t", t).wday - 1
    if weekNum == 0 then
        weekNum = 7
    end
    return weekNum;
end

function getMonthDay(...)
    -- 获取当月总天数
    local year = os.date("%Y", os.time()) -- 正常是获取服务器给的时间来算
    local month = os.date("%m", os.time()) + 1
    local dayAmount = os.date("%d", os.time({year = year, month = month, day = 0}))
    return dayAmount;
end

-- * @description: 通过某一时间点获取时间
-- * @params: @futureDays:0代表的意思是当天,1是明天,@_hour:指的24格式的时间,传入2就是凌晨2点
-- * @return: 时间戳
function GetFutureTime(futureDays, _hour)
    local curTimestamp = os.time()
    local dayTimestamp = 24 * 60 * 60
    local newTime = curTimestamp + dayTimestamp * futureDays
    local newDate = os.date("*t", newTime)
    --这里返回的是你指定的时间点的时间戳
    return os.time({year = newDate.year, month = newDate.month, day = newDate.day, hour = _hour, minute = newDate.minute, second = newDate.second})
end

function GetLastTime(datetype)
    --获取当前时间戳
    local curTimestamp = os.time()
    local nowDate = os.date("*t", curTimestamp);
    local nowDay = nowDate.day;
    local result;
    if(datetype == "TODAY")then
        --当天
        local newDate = {year = nowDate.year, month = nowDate.month, day = nowDate.day, hour = 24, minute = 0, second = 0}
        result = os.time(newDate);
       CS.TimeControl.Instance:GetNowServerTime(), curTimestamp, result, result - curTimestamp);

        
    elseif(datetype == "WEEK")then
        --本周 
        local weekNum = 7 - getWeekNum();
        result = GetFutureTime(weekNum, 24); 

    elseif(datetype == "MONTH")then
        --本月
        local totalDay = getMonthDay(); 
        local newDate = {year = nowDate.year, month = nowDate.month, day = totalDay, hour = 24, minute = 0, second = 0} 

        result = os.time(newDate);
    end
    return result - curTimestamp;
end

使用:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值