介绍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

lua时间戳和日期转换及踩坑的更多相关文章

  1. php日期转时间戳,指定日期转换成时间戳

    写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储.处理方便,但 是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么 ...

  2. php时间戳与日期转换

    日期转换为时间戳 PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是: strtotime():将任何英文文本的日期时间描述解析为时间戳. mktime():从日期取得时间戳. ...

  3. php时间戳和日期转换,以及时间戳和星期转换

    $this->created_at为时间戳值,转换日期如下 date('m.d',$this->created_at) :  y 代表年的后两位如 17 ,Y 代表 2017  , m 代 ...

  4. python 使用time / datetime进行时间、时间戳、日期转换

    python 使用time 进行时间.时间戳.日期格式转换 #!/usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2017/11/7 15:53 # ...

  5. jquery 时间戳与日期转换

    (function($) { $.extend({ myTime: { /** * 当前时间戳 * @return <int> unix时间戳(秒) */ CurTime: functio ...

  6. js中时间戳与日期转换-js日期操作

    常用的一些日期操作. 用js获取一个时间戳. <script type="text/javascript"> var date = new Date();//当前时间 ...

  7. PHP时间戳和日期转换

    获取当前时间 <?php var_dump(time()); //获取当前时间戳 int(1502245603) 时间戳转换为时间,可以用date(‘Y-m-s h:i:s’, 具体时间戳来实现 ...

  8. MySQL(Unix时间戳、日期)转换函数

    unix_timestamp() mysql> select unix_timestamp(); +------------------+ | unix_timestamp() | +----- ...

  9. php 时间戳与日期的转换(转载)

    UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式. a:Unix时间戳存储.处理方便,但是不直观 b:格式化日期直观,但是处理起来不如Unix时间戳那么自如 [关于两者的互相转换] 日  期 ...

随机推荐

  1. web开发的跨域问题详解

    本文由云+社区发表 做过 web 开发的同学,应该都遇到过跨域的问题,当我们从一个域名向另一个域名发送 Ajax 请求的时候,打开浏览器控制台就会看到跨域错误,今天我们就来聊聊跨域的问题. 1. 浏览 ...

  2. 通过keras例子理解LSTM 循环神经网络(RNN)

    博文的翻译和实践: Understanding Stateful LSTM Recurrent Neural Networks in Python with Keras 正文 一个强大而流行的循环神经 ...

  3. 无需操作系统直接运行 Python 代码

    Josh Triplett以一个“笑点”开始了他在PyCon 2015上的演讲:移植Python使其无需操作系统运行:他和他的英特尔同事让解释器能够在GRUB引导程序.BIOS或EFI系统上运行.连演 ...

  4. 系统不支持WP开发

    好伤心,,,系统不支持WP开发... 买的ThinkPad S5 自带的win8,既不属于专业版,也不属于家庭版,,不属于各种版本. 其他条件都满足了.. 难道我要还系统吗??

  5. websocket 初识

    websocket 初识 前言 其实很早就知道了 websocket 这个概念了,像现在大火的直播等使用的就是 websocket.之前找爬虫工作,对面问我爬过 websocket 网站没,很汗颜,那 ...

  6. python下载安装BeautifulSoup库

    python下载安装BeautifulSoup库 1.下载https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/ 2.解压到解压 ...

  7. c++中的new、operator new、placement new

    一.定义 1.new new是c++中的关键字,,其行为总是一致的.它先调用operator new分配内存,然后调用构造函数初始化那段内存. new 操作符的执行过程:1. 调用operator n ...

  8. [转]认识JWT

    本文转自:https://www.cnblogs.com/cjsblog/p/9277677.html 1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准 ...

  9. 把mysql脚本或其他数据库脚本导入Powerdesigner

    打开powerdesigner,选择File --> Reverse Engineer --> Database…… Model name是模型名称,DBMS选MySQL 5.0 然后确定 ...

  10. vb.net 水晶報表CrystalReport 動態設定資料庫來源

    沒有出現CrystalReportViewer時,須安裝CRforVS_13_0. 新增1個數據集,新增1個數據表,添加二列,列名要和資料庫名一樣. 修改目標Framework 修改app.confi ...