Python获取时间戳代码实例
作者:比特量化 时间:2023-11-13 04:02:52
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://www.cnblogs.com/fangbei/p/python-time.html
标签:python,获取,时间戳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mssql server .ldf和.mdf的文件附加数据库的sql语句
2024-01-14 21:56:13
python cv2图像质量压缩的算法示例
2023-08-16 19:09:02
![](https://img.aspxhome.com/file/2023/6/130966_0s.jpg)
d3.js入门教程之数据绑定详解
2024-04-22 12:48:54
Cpython3.9源码解析python中的大小整数
2021-11-17 05:45:42
浅谈Python 字符串格式化输出(format/printf)
2021-04-27 12:59:01
python中os模块和sys模块的使用详解
2021-08-29 21:42:35
![](https://img.aspxhome.com/file/2023/3/97403_0s.png)
Python自动化测试Eclipse+Pydev 搭建开发环境
2022-07-15 07:01:54
![](https://img.aspxhome.com/file/2023/1/107011_0s.png)
TensorFlow卷积神经网络AlexNet实现示例详解
2022-06-14 21:17:30
![](https://img.aspxhome.com/file/2023/3/133513_0s.png)
MySQL数据库优化经验详谈
2009-12-15 10:34:00
Python爬虫 批量爬取下载抖音视频代码实例
2021-08-02 23:25:10
![](https://img.aspxhome.com/file/2023/0/97340_0s.jpg)
将pymysql获取到的数据类型是tuple转化为pandas方式
2024-01-26 11:26:18
python中map的基本用法示例
2023-09-24 15:56:26
![](https://img.aspxhome.com/file/2023/1/124141_0s.png)
亲自教你使用 ChatGPT 编写 SQL JOIN 查询示例
2024-01-14 18:54:01
![](https://img.aspxhome.com/file/2023/6/105466_0s.png)
Python中使用第三方库xlrd来写入Excel文件示例
2023-08-16 21:37:29
基于python爬取有道翻译过程图解
2021-11-03 23:57:27
![](https://img.aspxhome.com/file/2023/9/109919_0s.png)
C# Ado.net实现读取SQLServer数据库存储过程列表及参数信息示例
2024-01-12 20:16:06
python 实现客户端与服务端的通信
2023-11-19 01:28:33
![](https://img.aspxhome.com/file/2023/7/101207_0s.png)
vue单页应用中如何使用jquery的方法示例
2024-05-09 10:40:14
Go语言中普通函数与方法的区别分析
2024-02-08 18:02:23
centos7使用rpm安装mysql5.7的教程图解
2024-01-21 20:07:09
![](https://img.aspxhome.com/file/2023/4/72704_0s.jpg)