1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| function print_r ( t ) local print_r_cache={} local function sub_print_r(t,indent) if (print_r_cache[tostring(t)]) then print(indent.."*"..tostring(t)) else print_r_cache[tostring(t)]=true if (type(t)=="table") then for pos,val in pairs(t) do if (type(val)=="table") then print(indent.."["..pos.."] => "..tostring(t).." {") sub_print_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] => "'..val..'"') else print(indent.."["..pos.."] => "..tostring(val)) end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub_print_r(t," ") print("}") else sub_print_r(t," ") end print() end
local nowTime = os.time() print("当前时间戳:"..nowTime)
local nowDate = os.date("%Y-%m-%d %H:%M:%S", nowTime) print("转换成时间:"..nowDate)
local nowTable = os.date("*t", nowTime) print("时间戳转为table:") print_r(nowTable)
print("table转为时间戳:".. os.time(nowTable))
print("自定义时间:"..os.time({year=2020, month=1, day=1,hour=0,min=0,sec=0}))
print("获取时间戳对应的日期")
function GetDayKeyByUnixTime(unixTime,hour) if hour == nil then hour = 0 end local retStr = os.date("%Y-%m-%d %H:%M:%S",unixTime) local time = unixTime local data = os.date("*t",time)
if data.hour < hour then time = time - 24*60*60 end
local data2 = os.date("*t",time) data2.hour = 0 data2.min = 0 data2.sec = 0
local time2 = os.time(data2)
local dayKey = os.date("%Y%m%d",time2) local timeBase = time2
return dayKey,retStr,timeBase end local time1 = os.time({year=2020,month=1,day=1}) local time2 = os.time({year=2020,month=1,day=10})
local dayKey,str = GetDayKeyByUnixTime(time1,4) print("dayKey = "..dayKey.." str = "..str) dayKey,str = GetDayKeyByUnixTime(time2,4) print("dayKey = "..dayKey.." str = "..str)
function NumberOfDaysInterval(unixTime1,unixTime2,dayFlagHour) if dayFlagHour == nil then dayFlagHour = 0 end local key1,str1,time1 = GetDayKeyByUnixTime(unixTime1,dayFlagHour) local key2,str2,time2 = GetDayKeyByUnixTime(unixTime2,dayFlagHour)
local sub = math.abs(time2 - time1)/(24*60*60) print(str1.." 与 "..str2.."相差的天数:"..sub)
return sub end
local sub = NumberOfDaysInterval(time1,time2,0) print(sub)
|