首页 技术 正文
技术 2022年11月18日
0 收藏 367 点赞 4,039 浏览 1441 个字

介绍lua的日期函数常用方法及我的一个踩坑。

时间戳转日期

os.date("%Y%m%d%H",unixtime)
--os.date("%Y%m%d%H",1534435200) 2018081700

日期转时间戳

---指定日期的时间戳
os.time({day=17, month=8, year=2018, hour=0, minute=0, second=0})
--1534435200

当前时间戳

os.time()

格式占位符

--时间格式 yyyyMMddHHmmss
print(os.date("%Y-%m-%d %H:%M %S", os.time()))
---输出 2019-01-30 10:47 53
print(os.date("%m月%d日 %H:%M", os.time())) --输出 01月30日 10:44

转成年月日接口

function Tool.FormatUnixTime2Date(unixTime)
if unixTime and unixTime >= 0 then
local tb = {}
tb.year = tonumber(os.date("%Y",unixTime))
tb.month =tonumber(os.date("%m",unixTime))
tb.day = tonumber(os.date("%d",unixTime))
tb.hour = tonumber(os.date("%H",unixTime))
tb.minute = tonumber(os.date("%M",unixTime))
tb.second = tonumber(os.date("%S",unixTime))
return tb
end
end

当然,如果你只需要拿天数进行比较,可以使用tonumber(os.date("%d",unixTime))

踩坑日志

不建议采用以下方式计算日期

function Tool.FormatDiffUnixTime2Tb(diffUnixTime)
if diffUnixTime and diffUnixTime >= 0 then
local tb = {}
---一天的秒数86400
tb.dd = math.floor(diffUnixTime / 60 / 60 / 24)
tb.hh = math.floor(diffUnixTime / 3600) % 24
tb.mm = math.floor(diffUnixTime / 60) % 60
tb.ss = math.floor(diffUnixTime % 60)
return tb
end
end

比如这两个零点日期,通过上述接口计算的dd是非常接近的!

日期 unix timestamp 计算值
2018/8/16 23:59:59 1534435199 17759.66665509259
2018/8/17 00:00:01 1534435201 17759.66667824074

转换计算工具

时间戳转换:http://tool.chinaz.com/Tools/unixtime.aspx

秒转成时间:http://cn.bestconverter.org/unitconverter_time.php

参考资料

https://www.cnblogs.com/Denny_Yang/p/6197435.html

http://www.cnblogs.com/whiteyun/archive/2009/08/10/1542913.html

http://blog.csdn.net/goodai007/article/details/8077285

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894