1. 程式人生 > 其它 >Lua獲取指定時間時間戳【判斷當前時間是否在某時間區間內】

Lua獲取指定時間時間戳【判斷當前時間是否在某時間區間內】

技術標籤:lua

廢話沒有,直接上程式碼


--獲取當日時間戳0點
function getTodayTimeStamp()

  local cDateCurrectTime = os.date("*t")

  local cDateTodayTime = os.time({year=cDateCurrectTime.year, month=cDateCurrectTime.month, day=cDateCurrectTime.day, hour=0,min=0,sec=0})

  return cDateTodayTime
end
--格式化時間為時間戳
function FormatTime(lastDate,day,hour)
	local dayTimestamp = 24* 60* 60
	lastDate = lastDate + dayTimestamp  *  day
	local date = os.date("*t", time)
	--這裡返回的是你指定的時間點的時間戳
	return os.time({year=date.year, month=date.month, day=date.day, hour=hour, minute = date.minute, second = date.second})
end

function main()
    --獲取當日時間戳0點
    local cDateTodayTime = getTodayTimeStamp()
    --當前時間戳
    local cDateCurrectTime = os.time()
    local cDateTodayTime_8 = FormatTime(cDateTodayTime,0,8.5)
    local cDateTodayTime_9 = FormatTime(cDateTodayTime,0,9.5)
    if cDateTodayTime_8 < cDateCurrectTime and cDateCurrectTime < cDateTodayTime_9 then
        print("now is 8:30--9:30 :now"..cDateCurrectTime..",8:30:"..cDateTodayTime_8..",9:30:"..cDateTodayTime_9)
    else
        print("now is not 8:30--9:30:now"..cDateCurrectTime..",8:30:"..cDateTodayTime_8..",9:30:"..cDateTodayTime_9)
    end
end

main()