其他分享
首页 > 其他分享> > 日期时间基本使用

日期时间基本使用

作者:互联网

时间戳转字符串

 1 function Test1()
 2     local now = os.time()
 3     print(os.date("%Y-%m-%d", now)) --2022-01-05
 4     print(os.date("%y-%m-%d", now)) --22-01-05
 5     print(os.date("%H:%M:%S", now)) --16:08:00
 6     print(os.date("%p %I:%M:%S", now)) --PM 04:08:00
 7     
 8     print(os.date("%x", now)) --01/05/22
 9     print(os.date("%c", now)) --01/05/22 16:08:00
10     
11     print(os.date("%b", now)) --Jan
12     print(os.date("%B", now)) --January
13     
14     print(os.date("%a", now)) --Wed
15     print(os.date("%A", now)) --Wednesday
16 end

 

时间戳转DateTime对象

 1 function Test2()
 2     local utcSec = os.time() --0时区
 3     
 4     local dateTime1 = os.date("*t", utcSec) --内部会加上时区
 5     local str1 = string.format("%s-%02d-%02d %02d:%02d:%02d", dateTime1.year, dateTime1.month, dateTime1.day, dateTime1.hour, dateTime1.min, dateTime1.sec)
 6     print(str1)
 7     
 8     local dateTime2 = os.date("!*t", utcSec) --内部不加时区
 9     local str2 = string.format("%s-%02d-%02d %02d:%02d:%02d", dateTime2.year, dateTime2.month, dateTime2.day, dateTime2.hour, dateTime2.min, dateTime2.sec)
10     print(str2)
11     
12     print(dateTime1.isdst) --夏令时
13     print(dateTime1.yday) --一年中的第几天
14     print(dateTime1.wday) --周几(1表示周日, 2表示周一, 7表示周六)
15 end

 

DateTime转时间戳

1 function Test3()
2     local dateTime = { year = 1970, month = 1, day = 1, hour = 8, min = 0, sec = 3 }
3     local utcSec = os.time(dateTime) --内部减掉时区
4     print(utcSec) --3(处在东8区时)
5 end

 

标签:基本,--,dateTime1,date,日期,使用,print,now,os
来源: https://www.cnblogs.com/sailJs/p/15844369.html