时间字符串比较
TimeCompare = TimeCompare or {} local TimeCompareT = TimeCompare function TimeCompareT.ParseTimeStr(timeStr) local pattern = "^(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)$" local index1, index2, year, month, day, h, m, s = string.find(timeStr, pattern) local timeComponents = { tonumber(year), tonumber(month), tonumber(day), tonumber(h), tonumber(m), tonumber(s), } return timeComponents end ---是否在时间范围内[startTime, endTime] function TimeCompareT.IsBetween(startTimeStr, endTimeStr, timeStr) local startTimeComponents = TimeCompareT.ParseTimeStr(startTimeStr) local endTimeComponents = TimeCompareT.ParseTimeStr(endTimeStr) local timeComponents = TimeCompareT.ParseTimeStr(timeStr) local isLtStart, isGtStart = false, false local isLtEnd, isGtEnd = false, false for i, v in ipairs(timeComponents) do if not isLtStart and not isGtStart then local startTimeComponent = startTimeComponents[i] if v < startTimeComponent then isLtStart = true elseif v > startTimeComponent then isGtStart = true end end if not isLtEnd and not isGtEnd then local endTimeComponent = endTimeComponents[i] if v > endTimeComponent then isGtEnd = true elseif v < endTimeComponent then isLtEnd = true end end -- > 开始时间 && < 结束时间 if isGtStart and isLtEnd then return true end -- < 开始时间 || > 结束时间 if isLtStart or isGtEnd then return false end end return true end
测试代码
print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-10 01:00:00")) -- 等于开始 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-10 00:59:00")) --之前1分钟 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-10 00:00:00")) --之前1小时 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-09 01:00:00")) --之前1天 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-01-10 01:00:00")) --之前1个月 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2023-02-10 01:00:00")) --之前1年 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2023-01-11 01:00:00")) --小1个月, 大1天 print("-----") print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-12 00:00:00")) --日期之间, 时间之前 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-12 11:00:00")) --日期之间, 时间之后 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-12 08:00:00")) --日期之间, 时间之间 print("-----") print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-15 10:00:00")) --等于结束 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-15 10:01:00")) --之后1分钟 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-15 11:00:00")) --之后1小时 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-02-16 10:00:00")) --之后1天 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2024-03-15 10:00:00")) --之后1个月 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2025-02-15 10:00:00")) --之后1年 print(TimeCompare.IsBetween("2024-02-10 01:00:00", "2024-02-15 10:00:00", "2025-03-14 10:00:00")) --大1个月, 小1天
运行结果


浙公网安备 33010602011771号