日期时间常用判断
作者:互联网
日期时间字符串转时间戳
1 function Parse_yyyyMMdd_hhmmss(dtStr) 2 --local str = "1970-01-01 08:00:03" 3 local pattern = "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)" 4 local index1, index2, year, month, day, h, m, s = string.find(dtStr, pattern) 5 6 local dateTime = { year = year, month = month, day = day, hour = h, min = m, sec = s } 7 return os.time(dateTime) 8 end
比如: 每天首次登录可领取一次奖励, 判断是否当天的首次登录
1 function NowBetweenDateTime(beginDtTimeStr, endDtTimeStr) 2 local utcSec1 = Parse_yyyyMMdd_hhmmss(beginDtTimeStr) 3 local utcSec2 = Parse_yyyyMMdd_hhmmss(endDtTimeStr) 4 5 local nowTimeStamp = os.time() 6 return nowTimeStamp >= utcSec1 and nowTimeStamp <= utcSec2 7 end
比如: 活动在每天的10:00开始, 13:30结束, 判断是否在活动时间内
1 function NowBetweenDayTime(beginTimeStr, endTimeStr) 2 local pattern = "(%d+):(%d+)" 3 4 local nowDateTime = os.date("*t", os.time()) 5 local nowDaySec = nowDateTime.hour * 3600 + nowDateTime.min * 60 6 7 local index1, index2, h, m = string.find(beginTimeStr, pattern) 8 local beginDaySec = h * 3600 + m * 60 9 if nowDaySec < beginDaySec then return false end 10 11 index1, index2, h, m = string.find(endTimeStr, pattern) 12 local endDaySec = h * 3600 + m * 60 13 if nowDaySec > endDaySec then return false end 14 15 return true 16 end
比如: 排行榜每天0点刷新, 距离刷新还剩多长时间
1 function LeftTime() 2 local dt = os.date("*t", os.time()) 3 local leftSec = 24 * 3600 - dt.hour * 3600 + dt.min * 60 + dt.sec 4 return leftSec 5 end
标签:常用,判断,return,os,日期,3600,pattern,end,local 来源: https://www.cnblogs.com/sailJs/p/15844384.html