1. 日期转秒:
datestr="2021-12-30 03:27"
pattern="(%d+)-(%d+)-(%d+) (%d+):(%d+)"
year,month,day,hour,min=datestr:match(pattern)
print(day, month, year, hour, min)
print(os.time({day=day, month=month, year=year, hour=hour, min=min, sec=0}))
> 30 12 2021 03 27
> 1640806020
---指定日期转为秒
print(os.time({day=17, month=8, year=2018, hour=0, minute=0, second=0}))
1534435200
2. 秒数转日期:
print(os.date("%Y-%m-%d %H:%M:%S",1479892620))
2016-11-23 17:17:00
os.time() 不传参的话返回当前时间转化成秒数的结果
print(os.time())
1640807175
print(os.date("%Y-%m-%d %H:%M:%S", os.time()))
2021-12-30 03:45:40
3. 时间戳转换:https://tool.lu/timestamp/
本文介绍了Python中日期字符串转秒、秒数转日期、以及时间戳处理的方法,包括`os.time()`和`os.date()`函数的应用,适合理解时间序列计算的基础开发者。
905

被折叠的 条评论
为什么被折叠?



