Python获取秒级时间戳与毫秒级时间戳的示例代码
作者:pengjunlee 时间:2022-05-05 22:13:00
1、获取秒级时间戳与毫秒级时间戳、微秒级时间戳
import time
import datetime
t = time.time()
print (t) #原始时间数据
print (int(t)) #秒级时间戳
print (int(round(t * 1000))) #毫秒级时间戳
print (int(round(t * 1000000))) #微秒级时间戳
1499825149.257892 #原始时间数据
1499825149 #秒级时间戳,10位
1499825149257 #毫秒级时间戳,13位
1499825149257892 #微秒级时间戳,16位
2、获取当前日期时间
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒的日期时间,来源 比特量化
print(dt)
print(dt_ms)
2018-09-06 21:54:46
2018-09-06 21:54:46.205213
3、将日期转为秒级时间戳
dt = '2018-01-01 10:40:30'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
print (ts)
1514774430
4、将秒级时间戳转为日期
ts = 1515774430
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
print(dt)
2018-01-13 00:27:10
5、时间格式转成另一种时间格式
dt = '08/02/2019 01:00'
dt_new = datetime.datetime.strptime(dt, '%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S')
print(dt_new)
2019-08-02 01:00:00
6、转结构体时间struct_time
ta_dt = time.strptime("2018-09-06 21:54:46", '%Y-%m-%d %H:%M:%S') #日期时间转结构体
ta_ms = time.localtime(1486188476) #时间戳转结构体,注意时间戳要求为int,来源 比特量化
print(ta_dt)
print(ta_ms)
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=6, tm_hour=21, tm_min=54, tm_sec=46, tm_wday=3, tm_yday=249, tm_isdst=-1)
time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=14, tm_min=7, tm_sec=56, tm_wday=5, tm_yday=35, tm_isdst=0)
来源:https://blog.csdn.net/pengjunlee/article/details/102719877
标签:Python,秒级时间戳,毫秒级时间戳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
innerHTML 的些摘记
2009-06-01 15:42:00
Flash对象在(x)HTML中的格式和参数及安全性
2010-04-01 11:55:00
![](https://img.aspxhome.com/file/UploadPic/20104/1/634042425122417500_1-54s.png)
python递归实现快速排序
2023-08-26 22:46:27
js实现QQ邮箱邮件拖拽删除功能
2023-09-07 04:31:05
如何在asp中创建DSN?
2009-11-14 20:46:00
pyppeteer执行js绕过webdriver监测方法上
2021-12-13 12:04:40
静态页面分页的AJAX实现
2008-01-19 16:19:00
![](https://img.aspxhome.com/file/UploadPic/20081/19/2008119162559319s.jpg)
Python实现随机创建电话号码的方法示例
2021-08-31 06:38:03
![](https://img.aspxhome.com/file/2023/5/67415_0s.png)
使用Python第三方库pygame写个贪吃蛇小游戏
2021-05-19 11:08:37
![](https://img.aspxhome.com/file/2023/1/76701_0s.png)
24式加速你的Python(小结)
2023-09-25 10:25:24
![](https://img.aspxhome.com/file/2023/2/62572_0s.png)
教你怎么用Python处理excel实现自动化办公
2023-11-05 02:19:50
![](https://img.aspxhome.com/file/2023/1/64301_0s.png)
PyHacker编写指南引用Nmap模块实现端口扫描器
2023-07-15 14:15:40
![](https://img.aspxhome.com/file/2023/2/63602_0s.png)
JS实现运动缓冲效果的封装函数示例
2023-06-30 14:34:45
Python实战之画哆啦A梦(超详细步骤)
2021-05-16 14:36:47
![](https://img.aspxhome.com/file/2023/0/80480_0s.jpg)
Python GUI Tkinter简单实现个性签名设计
2022-05-22 16:04:08
![](https://img.aspxhome.com/file/2023/1/68911_0s.png)
mysql rand(x)生产重复随机序列
2010-12-03 16:01:00
基于PHP实现解密或加密Cloudflar邮箱保护
2023-07-22 13:22:37
JavaScript实现全选取消效果
2023-08-27 16:26:42
![](https://img.aspxhome.com/file/2023/7/56157_0s.png)
颜色渐变效果
2013-07-13 14:14:52
基于Python组装jmx并调用JMeter实现压力测试
2023-05-03 04:42:20