介绍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. IDEA的几个常用配置,日常开发必备。

    用了IDEA有很长时间了,身边的同事朋友也都慢慢的开始都从Eclipse切换到IDEA了,其实无论是Eclipse还是IntelliJ IDEA都是开发工具而已,各自都有优点.但是刚从Eclipse切 ...

  2. python模块之pickle、shelve、json

    一 什么是序列化 序列化指的是将内存中的数据结构转化为一种中间格式,并存储到硬盘上. (反序列化:将硬盘上存储的中间格式数据再还原为内存中的数据结构) 二 为什么要序列化 持久保持状态 需知一个软件/ ...

  3. Code First下迁移数据库更改

    第一步:Enable-Migrations -ContextTypeName [你的项目名].[你的数据库上下文] -Force 其中-Force为强制覆盖现有迁移配置 第二步:Add-Migrati ...

  4. Win10一周年纪念版,瞧一瞧Linux子系统

    Bash Bash是一个命令处理器,通常运行于文本窗口中,并能执行用户直接输入的命令.Bash还能从文件中读取命令,这样的文件称为脚本.和其他Unix shell 一样,它支持文件名替换(通配符匹配) ...

  5. AnnotationConfigBeanDefinitionParser are only available on JDK 1.5 and higher

    報錯: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML ...

  6. Maven(七)Eclipse使用Maven命令

    由于没有mvn compile (其余命令类似) 可以点解上面框中选项手动输入compile

  7. Cheating sheet for vim

  8. 查看linux 服务器还剩多少空间

    df -hl 或者 df -m

  9. 使用eclipse初步学习vue.js基础==》v-for的使用 ②

    一.步骤演示 1. 新建一个jsp文件 2. 把vue.js放到Web的js目录下 3. 在jsp中引入vue.js <script src="${pageContext.reques ...

  10. angular $index获取ng-repeat的上一条数据

    <div ng-repeat="item in dataList" ng-click="func($index,$index-1)"></di ...