lua比较两个时间点,返回相差几天
2017-03-07 12:01
176 查看
-- 上一个时间 local lastYear = os.date("%Y", 1490889600) local lastMonth = os.date("%m", 1490889600) local lastDay = os.date("%d", 1490889600) print("lastYear==="..lastYear) print("lastMonth==="..lastMonth) print("lastDay==="..lastDay) -- 当前时间 local curYear = os.date("%Y", 1490976000) local curMonth = os.date("%m", 1490976000) local curDay = os.date("%d", 1490976000) print("curYear==="..curYear) print("curMonth==="..curMonth) print("curDay==="..curDay) --[[比较两个时间,返回相差多少时间]] function timediff(long_time,short_time) local n_short_time,n_long_time,carry,diff = os.date('*t',short_time),os.date('*t',long_time),false,{} local colMax = {60,60,24,os.date('*t',os.time{year=n_short_time.year,month=n_short_time.month+1,day=0}).day,12,0} n_long_time.hour = n_long_time.hour - (n_long_time.isdst and 1 or 0) + (n_short_time.isdst and 1 or 0) -- handle dst for i,v in ipairs({'sec','min','hour','day','month','year'}) do diff[v] = n_long_time[v] - n_short_time[v] + (carry and -1 or 0) carry = diff[v] < 0 if carry then diff[v] = diff[v] + colMax[i] end end return diff end local n_long_time = os.date(os.time{year=curYear,month=curMonth,day=curDay,hour=0,min=0,sec=0}); local n_short_time = os.date(os.time{year=lastYear,month=lastMonth,day=lastDay,hour=0,min=0,sec=0}); local t_time = timediff(n_long_time,n_short_time); local time_txt = string.format("%04d", t_time.year).."年"..string.format("%02d", t_time.month).."月"..string.format("%02d", t_time.day).."日 "..string.format("%02d", t_time.hour)..":"..string.format("%02d", t_time.min)..":"..string.format("%02d", t_time.sec); print(time_txt);
相关文章推荐
- 【Android】两个日期相差几天和两个日期比较大小
- 两个日期的比较, 相差多少秒,多少小时,多少分钟,都是getTime()x先获取秒,在做处理--Math.ceil(long)返回一个大于这个数的最小整数
- 比较两个日期相差几天
- 比较两个date返回日期相差天数
- 如何比较两个日期相差几天
- LUA 比较两个时间点(os.date())之间的时间间隔值
- 如何比较两个日期相差几天,(更简单的算法)
- Java比较两个时间相差多少天,多少个月,多少年
- 写一个函数,输入一个二叉树,树中每个节点存放了一个整数值,函数返回这棵二叉树中相差最大的两个节点间的差值绝对值。
- PHP资料 PHP比较两个时间相差的秒数通过strtotime
- Lua中返回两个值
- java equals 比较两个字符串,总是返回false
- Java计算两个日期时间相差几天,几小时,几分钟等
- 写一个函数,输入一个二叉树,树中每一个节点存放了一个整数值,函数返回这棵二叉树中相差最大的两个节点间的差值绝对值。请注意程序效率。
- C# 两个datatable中的数据快速比较返回交集或差集
- JAVA 比较两个日期相差的天数
- 怎么计算两个日期之间相差几天及时几分
- 比较两个DataTable数据(结构相同),返回新增的,删除的,修改前的,修改后的 DataTable
- asp.net 比较两个时间,返回时间间隔