找回密码
 会员注册
查看: 19|回复: 0

Python时间和时间戳相互转换

[复制链接]

2

主题

0

回帖

7

积分

新手上路

积分
7
发表于 2024-9-8 14:30:45 | 显示全部楼层 |阅读模式
importtypingastimporttimeimportdatetime"""原文:python时间相互转换https://py-code.readthedocs.io/zh/latest/Python/time_utils/index.html""""""#1.1.时间字符串转换为13位时间戳#1.2.时间字符串转换为datetime#1.3.时间戳转换为时间字符串#1.4.时间戳转换为datetime对象#1.5.datetime对象转换为时间字符串#1.6.datetime对象转换为13位时间戳"""#1.1.时间字符串转换为13位时间戳defstr_to_timestamp(str_time:str,time_format:str="%Y-%m-%d%H:%M:%S")->int:"""时间字符串转换为13位时间戳:paramstr_time:时间字符串:paramtime_format:时间字符串格式default:%Y-%m-%d%H:%M:%Sexample:%Y-%m-%d:return:13位时间戳Usage::>>>str_to_timestamp("2022-10-10","%Y-%m-%d")1665331200000>>>str_to_timestamp("2022-10-1010:10:10")1665367810000"""try:time_array=time.strptime(str_time,time_format)returnint(time.mktime(time_array))*1000exceptValueError:raiseValueError("Invalidtimeformat!")#1.2.时间字符串转换为datetimedefstr_to_datetime(str_time:str)->datetime.datetime:"""时间字符串转换为datetime:paramstr_time:时间字符串格式为"2022-10-1010:10:10"或"2022-10-10":return:datetime对象Usage::>>>str_to_datetime("2022-10-1010:10:10")2022-10-1010:10:10>>>str_to_datetime("2022-10-10")2022-10-1000:00:00"""try:if""instr_time:returndatetime.datetime.strptime(str_time,"%Y-%m-%d%H:%M:%S")else:returndatetime.datetime.strptime(str_time,"%Y-%m-%d")exceptValueError:raiseValueError("Invalidtimeformat!")#1.3.时间戳转换为时间字符串deftimestamp_to_str(timestamp:int,time_format:str="%Y-%m-%d%H:%M:%S")->str:"""时间戳转换为时间字符串:paramtimestamp:时间戳:paramtime_format:时间字符串格式default:%Y-%m-%d%H:%M:%S:return:时间字符串Usage::>>>timestamp_to_str(1665331200000,"%Y-%m-%d")2022-10-10>>>timestamp_to_str(1665367810000)2022-10-1010:10:10"""try:datetime_type=datetime.datetime.fromtimestamp(timestamp//1000)returndatetime_type.strftime(time_format)except(TypeError,ValueError):raiseValueError("Invalidtimestampformat!")#1.4.时间戳转换为datetime对象deftimestamp_to_datetime(timestamp:t.Union[int,float])->datetime.datetime:"""时间戳转换为datetime对象:paramtimestamp:时间戳:return:datetime对象Usage::>>>timestamp_to_datetime(1645513117000)2022-02-2214:58:37>>>timestamp_to_datetime(1429417200.0)2015-04-1912:20:00"""try:#13位时间戳毫秒格式转换iflen(str(int(timestamp)))==13:returndatetime.datetime.fromtimestamp(timestamp//1000)returndatetime.datetime.fromtimestamp(timestamp)exceptValueError:raiseValueError("Invalidtimeformat!")#1.5.datetime对象转换为时间字符串defdatetime_to_str(datetime_obj:datetime.datetime)->str:"""datetime对象转换为时间字符串:paramdatetime_obj:datetime对象:return:时间字符串Usage::>>>datetime_to_str(datetime.datetime.now()))2022-02-2214:46:04"""try:returndatetime_obj.strftime("%Y-%m-%d%H:%M:%S")exceptAttributeError:raiseValueError("Invalidtimeformat!")#1.6.datetime对象转换为13位时间戳defdatetime_to_timestamp(datetime_obj:datetime.datetime)->int:"""datetime对象转换为13位时间戳:paramdatetime_obj:datetime对象:return:13位时间戳Usage::>>>datetime_to_timestamp(datetime.datetime.now()))1645513117000"""try:returnint(datetime_obj.timestamp())*1000exceptAttributeError:raiseValueError("Invalidtimeformat!")调用:#1.1.时间字符串转换为13位时间戳print(str_to_timestamp("2022-10-10","%Y-%m-%d"))print(str_to_timestamp("2022-10-1010:10:10"))'''输出:16653312000001665367810000'''#1.2.时间字符串转换为datetimeprint(str_to_datetime("2022-10-1010:10:10"))print(str_to_datetime("2022-10-10"))'''输出:2022-10-1010:10:102022-10-1000:00:00'''#1.3.位时间戳转换为时间字符串print(timestamp_to_str(1665331200000,"%Y-%m-%d"))print(timestamp_to_str(1665367810000))'''输出:2022-10-102022-10-1010:10:10'''#1.4.时间戳转换为datetime对象print(timestamp_to_datetime(1645513117000))print(timestamp_to_datetime(1429417200.0))'''输出:2022-02-2214:58:372015-04-1912:20:00'''#1.5.datetime对象转换为时间字符串print(datetime_to_str(datetime.datetime.now()))'''输出:2024-06-2113:43:46'''#1.6.datetime对象转换为13位时间戳print(datetime_to_timestamp(datetime.datetime.now()))'''输出:1718948690000'''
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-11 01:31 , Processed in 0.456701 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表