Lua通过某一时间点获取时间戳

-- * @description: 通过某一时间点获取时间
-- * @params: @futureDays:0代表的意思是当天,1是明天,@_hour:指的24格式的时间,传入2就是凌晨2点
-- * @return: 时间戳
function Tool: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
self:GetFutureTime(1, 0)

打印结果:1607443200

进行拓展获取剩余时间:

-- * @description: 转换为天时间
-- * @params: @_time:秒数
-- * @return:返回剩余时间,格式如:05:07:16:33
-- * @date: 2020-12-7 11:10
function ToolKit:FormatUnixTime(_time)
    if _time and _time >= 0 then
        ---一天的秒数86400
        local day = math.floor(_time / 60 / 60 / 24)
        --小于10 需要十位补0
        local hour = math.floor(_time / 3600) % 24
        local minute = math.floor(_time / 60) % 60
        local second = math.floor(_time % 60)
        local dayStr = day < 10 and "0" .. day or day
        local hourStr = hour < 10 and "0" .. hour or hour
        local minuteStr = minute < 10 and "0" .. minute or minute
        local secondStr = second < 10 and "0" .. second or second
        return dayStr .. "" .. hourStr .. "" .. minuteStr .. "" .. secondStr
    end
end

通过string.format进行优化后:

-- * @description: 转换为天时间
-- * @params: @_time:秒数
-- * @return:返回剩余时间,格式如:05:07:16:33
-- * @date: 2020-12-7 11:10
-- * @author: 云更
function ToolKit:FormatUnixTime(_time)
    if _time and _time >= 0 then
        --一天的秒数86400
        local day = math.floor(_time / 86400)
        local hour = math.fmod(math.floor(_time / 3600), 24);
        local minute = math.fmod(math.floor(_time / 60), 60)
        local second = math.fmod(_time, 60)
        --小于10 需要十位补0 ,%02d 格式化为至少2位十进制整数 
        return string.format("%02d:%02d:%02d:%02d", day, hour, minute, second)
    end
end

 拓展:获取通过某一时间点获取时间(获取隔周的时间)

-- * @description: 转换为小时时间
-- * @params: @_time:秒数
-- * @return:返回剩余时间,格式如:07:16:33
-- * @date: 2020-12-7 11:10
function ToolKit:FormatHourUnixTime(_time)
    if _time and _time >= 0 then
        local hour = math.fmod(math.floor(_time / 3600), 24);
        local minute = math.fmod(math.floor(_time / 60), 60)
        local second = math.fmod(_time, 60)
        --小于10 需要十位补0 ,%02d 格式化为至少2位十进制整数 
        return string.format("%02d:%02d:%02d", hour, minute, second)
    end
end
-- * @description: 获得明天零点的时间戳
-- * @params: @_timerStamp:当前的时间
-- * @return: 明天零点的时间戳
-- * @date: 2020-12-15 16:27
function ToolKit:GetNextDayZeroTimestamp(_timerStamp)
    --获得当前的时间
    local timeStamp = _timerStamp
    if not timeStamp then
        timeStamp = os.time()
    end
    --获得时间格式
    local formatTime = os.date("*t", timeStamp)
    formatTime.hour = 23
    formatTime.min = 59
    formatTime.sec = 59
    --获得第二天零点的时间戳
    local nextTimestamp = os.time(formatTime) + 1
    return nextTimestamp
end

-- * @description: 获得当天零点的时间戳
-- * @params: @_timerStamp:当前的时间
-- * @return: 明天零点的时间戳
-- * @date: 2020-12-15 16:27
function ToolKit:GetCurrentDayZeroTimestamp(_timerStamp)
    --获得当前的时间
    local timeStamp = _timerStamp
    if not timeStamp then
        timeStamp = os.time()
    end
    --获得时间格式
    local formatTime = os.date("*t", timeStamp)
    formatTime.hour = 0
    formatTime.min = 0
    formatTime.sec = 0
    --获得第二天零点的时间戳
    local curTimestamp = os.time(formatTime)
    return curTimestamp
end

-- * @description: 通过某一时间点获取时间(获取隔周的时间戳)
-- * @params: @updateTime:更新时间(几点0-24)
-- * @return: @residueStr:剩余天数(05:16:14:22(天:时:分:秒));@diffTimestamp:剩余时间
-- * @date: 2020-12-15 10:45
function ToolKit:GetFutureWeekTime(updateTime)
    if not updateTime then
        updateTime = 0
    end
    --获得当前服务器的时间
    local curTimestamp = os.time()
    --获得时间格式  {"day":15,"hour":15,"isdst":false,"min":34,"month":12,"sec":13,"wday":3,"yday":350,"year":2020}
    local curTimeFormat = os.date("*t", curTimestamp)
    local nextTimestamp = 0
    if curTimeFormat.hour < updateTime then
        --当时间小于更新时间
        nextTimestamp = ToolKit:GetCurrentDayZeroTimestamp(curTimestamp) + updateTime * 3600
    else
        --获得第二天零点的时间戳
        nextTimestamp = ToolKit:GetNextDayZeroTimestamp(curTimestamp) + updateTime * 3600
    end
    --剩余天数
    local residueDay = 0
    --到本周末还有多少时间
    if curTimeFormat.wday == 1 then
        --周日
        residueDay = curTimeFormat.hour < updateTime and 6 or 7
    elseif curTimeFormat.wday == 2 then
        --周一
        residueDay = curTimeFormat.hour < updateTime and 7 or 1
    else
        residueDay = curTimeFormat.hour < updateTime and curTimeFormat.wday - 2 or curTimeFormat.wday - 1
    end
    residueDay = 7 - residueDay
    --转换显示格式
    local residueStr = ""
    local diffTimestamp = os.difftime(nextTimestamp, curTimestamp)
    local temp = ToolKit:FormatHourUnixTime(diffTimestamp)
    if residueDay > 0 then
        residueStr = string.format("%02d:%s", residueDay, temp)
    else
        residueStr = ToolKit:FormatUnixTime(diffTimestamp)
    end
    return residueStr, diffTimestamp
end
原文地址:https://www.cnblogs.com/jbw752746541/p/14105442.html