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()

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Lua获取毫秒级时间戳,可以使用luasocket库来实现。首先,你需要下载luasocket库并将其添加到你的Lua环境中。然后,你可以使用以下代码来获取当前的毫秒级时间戳: ```lua require("socket") local current_time = socket.gettime() * 1000 ``` 这里,我们使用了luasocket提供的`socket.gettime()`函数来获取当前时间的秒级时间戳,然后乘以1000来转换为毫秒级时间戳。`current_time`变量将保存当前的毫秒级时间戳。 请注意,获取毫秒级时间戳需要使用luasocket库,并且在指定日期之前无法获取时间戳,否则将返回`nil` 。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Lua中使用毫秒精度时间的方法](https://blog.csdn.net/liuxing__jacker/article/details/118065458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Lua 使用 os.time 获取时间戳需谨慎](https://blog.csdn.net/fightsyj/article/details/118156531)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值