-- local getTime = os.date(“%c”);
-- %a abbreviated weekday name (e.g., Wed)
-- %A full weekday name (e.g., Wednesday)
-- %b abbreviated month name (e.g., Sep)
-- %B full month name (e.g., September)
-- %c date and time (e.g., 09/16/98 23:48:10)
-- %d day of the month (16) [01-31]
-- %H hour, using a 24-hour clock (23) [00-23]
-- %I hour, using a 12-hour clock (11) [01-12]
-- %M minute (48) [00-59]
-- %m month (09) [01-12]
-- %p either “am” or “pm” (pm)
-- %S second (10) [00-61]
-- %w weekday (3) [0-6 = Sunday-Saturday]
-- %x date (e.g., 09/16/98)
-- %X time (e.g., 23:48:10)
-- %Y full year (1998)
-- %y two-digit year (98) [00-99]
-- %% the character ‘%’
-- 如获取当前年月日时分秒:local date=os.date(“%Y-%m-%d %H:%M:%S”);
--
-- local date = os.date("%Y-%m-%d %H:%M:%S")
------------------------------计算时间差-----------------------------
local currentTime = os.date(os.time())
local CoolingTime = currentTime - 100
local s = os.difftime(currentTime, CoolingTime)
local str = string.format("%2d:%2d:%2d", s/(60*60), s/60%60, s%60)
print(str)
local s = os.difftime(currentTime, CoolingTime)
local str = os.date("%X", s)
print(str)
----------------------------获取指定时间值-----------------------------
-- 通过一个table参数, 指定一个时间,返回时间的数值格式
time = os.time(tab)
local tab = {year=2005, month=11, day=6, hour=22,min=18,sec=30,isdst=false}
time = os.time(tab)
-- 获取当前时间的table格式
tab = os.date("*t")
--year表示年,month表示月,day表示日期,hour表示小时,min表示分钟,sec表示秒,isdst表示是否夏令时
--tab成包括一些其他的成员 tab.yday 表示一年中的第几天 tab.wday 表示星期几(星期天为1)
---------------------------------------------------------------------
local GameLogic = commonlib.gettable("Pisces.GameLogic") local TimeTool = {} commonlib.TimeTool = TimeTool local HOUR_OF_DAY = 24 local SECOND_OF_MINUTE = 60 local SECOND_OF_HOUR = 3600 local SECOND_OF_DAY = HOUR_OF_DAY * SECOND_OF_HOUR function TimeTool:GetServerUnixTime( ) return GameLogic:GetServerUnixTime() end function TimeTool:GetMillSecond( ) return cc.Director:getMillSecond() end --往前追朔到某一天的零点, 单位秒数 function TimeTool:GetDayBegin(time) time = time or self:GetServerUnixTime() local timetable = os.date("*t", time) timetable.hour=0 timetable.min =0 timetable.sec = 0 return os.time(timetable) end --当前是一天的第几秒 function TimeTool:GetSecondsOfDay( ) local now = self:GetServerUnixTime() local zero = self:GetDayBegin(now) return now - zero end --将秒数转换为:00:00:00 function TimeTool:TimeToString(time, flag) time = time or self:GetServerUnixTime() local sec = time % SECOND_OF_MINUTE local min = math.floor(time / 60) % 60 local hour = math.floor(time / SECOND_OF_HOUR) if flag == 1 then return string.format("%02d:%02d:%02d", hour, min, sec) elseif flag == 2 then return string.format("%02d:%02d", min, sec) end end --获取间隔的天数 function TimeTool:GetIntervalDay(timeBegin, tiemEnd) local dayBegTime1 = self:GetDayBegin(timeBegin) local dayBegTime2 = self:GetDayBegin(tiemEnd) return math.floor((math.abs(dayBegTime2 - dayBegTime1)) / SECOND_OF_DAY) end -- 检查两个时间是否为同一天 function TimeTool:CheckTheSameDay(time1, time2) return self:GetIntervalDay(time1, time2) == 0 end --得到自定义“天”的初始时间点, dayHour, dayMin, daySec为自定义天的跨越点 function TimeTool:GetUserDayBegTime(time, dayHour, dayMin, daySec) time = time or self:GetServerUnixTime() dayHour = dayHour or 0 dayMin = dayMin or 0 daySec = daySec or 0 local tbTime local curHour = tonumber(os.date("%H", time)) if curHour < dayHour then local nTime = time - 60*60*24 tbTime = os.date("*t", nTime) else tbTime = os.date("*t", time) end tbTime.hour = dayHour tbTime.min= dayMin tbTime.sec = daySec return os.time(tbTime) end --判断两个时间是否在同一“天” function TimeTool:DiffUserDay(time1, time2, dayHour, dayMin, daySec) dayHour = dayHour or 0 dayMin = dayMin or 0 daySec = daySec or 0 local DayBegTime1 = self:GetUserDayBegTime(time1, dayHour, dayMin, daySec) local DayBegTime2 = self:GetUserDayBegTime(time2, dayHour, dayMin, daySec) return DayBegTime1 == DayBegTime2 end