(原创)lua日期、时间、时间戳的计算和转换
----------------------------------------------日期与时间
print("当前时间戳:")
local nowTime = os.time()
print(nowTime) print("")
print("转换成日期:")
--时间戳 转 日期
local nowData = os.date("%Y%m%d%H%M%S",nowTime)
print(nowData) --可以单独转换
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)) --转成table
local tb = os.date("*t",nowTime)
dump(tb) print("")
print("日期转回时间戳:")
print(os.time(tb)) print("") local time1 = os.time({year=,month=,day=,hour=, min=, sec=})
local time2 = os.time({year=,month=,day=,hour=, min=, sec=}) print("获取时间戳对应的日期")
--获取时间戳对应的天数(天数规则不是自然天,是(hour)4点前当前一天算)
function Tool:GetDayKeyByUnixTime(unixTime,hour)
if hour == nil then hour = end
local retStr = os.date("%Y-%m-%d %H:%M:%S",unixTime)
local time = unixTime
local data = os.date("*t",time)
--dump(data) --(hour)4点前按前一天算
if data.hour < hour then
time = time - **
end local data2 = os.date("*t",time)
--dump(data2)
data2.hour =
data2.min =
data2.sec = local time2 = os.time(data2) local dayKey = os.date("Key%Y%m%d",time2)
local timeBase = time2 --天数key,日期格式字符串,天数key 0点的时间戳
return dayKey,retStr,timeBase
end local dayKey,str = Tool:GetDayKeyByUnixTime(time1,)
print("dayKey = "..dayKey.." str = "..str)
dayKey,str = Tool:GetDayKeyByUnixTime(time2,)
print("dayKey = "..dayKey.." str = "..str) --两个时间的天数差 --时间戳1 时间戳2 多少点开始算第二天
function Tool:NumberOfDaysInterval(unixTime1,unixTime2,dayFlagHour)
if dayFlagHour == nil then dayFlagHour = end
local key1,str1,time1 = Tool:GetDayKeyByUnixTime(unixTime1,dayFlagHour)
local key2,str2,time2 = Tool:GetDayKeyByUnixTime(unixTime2,dayFlagHour) local sub = math.abs(time2 - time1)/(**)
print(str1.." 与 "..str2.."相差的天数:"..sub) return sub
end local sub = Tool:NumberOfDaysInterval(time1,time2,)
print(sub)
(原创)lua日期、时间、时间戳的计算和转换的更多相关文章
- c/c++日期时间处理与字符串string转换
转自:https://www.cnblogs.com/renjiashuo/p/6913668.html 在c/c++实际问题的编程中,我们经常会用到日期与时间的格式,在算法运行中,通常将时间转化为i ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- Java开发笔记(四十四)本地日期时间与字符串的互相转换
之前介绍Calendar的时候,提到日历实例无法直接输出格式化后的时间字符串,必须先把Calendar类型转换成Date类型,再通过格式化工具SimpleDateFormat获得字符串.而日期时间的格 ...
- C语言 strftime 格式化显示日期时间 时间戳
C/C++程序中需要程序显示当前时间,可以使用标准函数strftime. 函数原型:size_t strftime (char* ptr, size_t maxsize, const char* fo ...
- 字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: “2019-09-27T18:31:31+08:00”
错误的写法: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这里的格式也可以是别 ...
- 摘:C++日期时间与字符串间的转换
VC6中 CString sTime = _T("2007-10-26 13:20:30"); char *charTime = (LPSTR)(LPCTSTR)sTime; CS ...
- Java 日期时间与unix时间戳之间转换
日期时间 <--> 时间戳 java.time 包提供的新的日期和时间API LocalDateTime: 本地日期时间类 ZoneId: 时区类 ZonedDateTime: 带时区 ...
- ORACLE函数之日期时间运算函数
1 ADD_MONTHS 格式:ADD_MONTHS(D,N) 说明:返回日期时间D加N月后相应的日期时间.N为正时则表示D之后:N为负时则表示为D之前.N为小数则会自己主动先删 ...
- Oracle中与日期时间有关的运算函数
1 ADD_MONTHS 格式:ADD_MONTHS(D,N) 说明:返回日期时间D加N月后对应的日期时间.N为正时则表示D之后:N为负时则表示为D之前:N为小数则会自动先删除小 ...
随机推荐
- H5软键盘兼容方案
前言 最近一段时间在做 H5 聊天项目,踩过其中一大坑:输入框获取焦点,软键盘弹起,要求输入框吸附(或顶)在输入法框上.需求很明确,看似很简单,其实不然.从实验过一些机型上看,发现主要存在以下问题: ...
- spring整合quartz异常:org.quartz.JobPersistenceException: Couldn't clean volatile data: Unknown column 'IS_VOLATILE' in 'where clause'
自己的SSM项目中要用到定时器,初期使用Timer,后来用spring 的schedule,都比较简单,所以功能比较单一.后来就研究quartz,准备整合到项目中.遇到了异常,异常内容如下: [201 ...
- 台达wplsoft2.34指令表
常用: LD 载入 A 接点 LDI 载入 B 接点 AND 串联 A 接点 ANI 串联 B 接点 OR 并联 A 接点 ORI 并联 B 接点 ANB 串联回路方块 ORB 并联回路方块 MPS ...
- C#进行回文检测,判断字符串是否是回文的代码
下面代码内容是关于C#进行回文检测,判断字符串是否是回文的代码,应该是对各位朋友有些好处. Console.WriteLine("算法1:请输入一个字符串!");string st ...
- celery概述
celery介绍 Celery是一个功能完备即插即用的任务队列.它使得我们不需要考虑复杂的问题,使用非常简单.celery看起来似乎很庞大,本章节我们先对其进行简单的了解,然后再去学习其他一些高级特性 ...
- docker上传自己的镜像
https://blog.csdn.net/boonya/article/details/74906927 需要注意的就是命名规范 docker push 注册用户名/镜像名
- <Android基础> (六) 数据存储 Part 2 SharedPreferences方式
6.3 SharedPreferences存储 SharedPreferences使用键值对的方式来存储数据.同时支持多种不同的数据类型. 6.3.1 将数据存储到SharedPreferences中 ...
- Inheritance: 'A' is an inaccessible base of 'B'
'boost::enable_shared_from_this<net::Session>' is an inaccessible base of 'net::Session' BOOST ...
- Vue-router(基础)_滚动行为和history模式
一.前言 1.滚动事件 2.h5 history模式 二.主要内容 1. (1)使用前度路由,当切换到新路由时,想要页面滚动到顶部,或者是保持原先滚动的位置,就像重新加载页面那样.vue-rout ...
- Mysql查询库、表存储量(Size)
Mysql查询库.表存储量(Size) 1.要查询表所占的容量,就是把表的数据和索引加起来就可以了. SELECT SUM(DATA_LENGTH) + SUM(INDEX_LENGTH) FROM ...