获取此刻到当天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
使用: