Python工具类函数—时间转换处理

本文介绍了Python中处理时间的方法,包括获取当前时间、转换时间格式、时间戳的相互转换以及时间的加减运算。通过示例代码展示了如何实现这些操作,如datetime模块和time模块的使用,以及如何获取当天凌晨时间戳和一分钟后的时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

datetime

获取当前时间

from datetime import datetime

t1 = datetime.now()
print(t1, type(t1))
# 2022-05-17 11:26:58.033436 <class 'datetime.datetime'>

datetime转字符串

t2 = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(t2, type(t2))
# 2022-05-17 11:29:06 <class 'str'>

字符串转datetime

t3 = datetime.strptime('2022-05-17 11:29:06', '%Y-%m-%d %H:%M:%S')
print(t3, type(t3))
# 2022-05-17 11:29:06 <class 'datetime.datetime'>

时间戳转datetime

t1 = datetime.datetime.strptime(
    time.strftime("%Y.%m.%d", time.localtime(1692633600)), "%Y.%m.%d"
)
print(t1, type(t1))
# 2023-08-22 00:00:00 <class 'datetime.datetime'>

获取精确到秒级的当前时间

t4 = datetime.strptime(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
print(t4, type(t4))
# 2022-05-17 11:32:44 <class 'datetime.datetime'>

获取时间戳

t5 = datetime.now().timestamp()
print(t5, type(t5))
# 1652758498.971751 <class 'float'>

获取精确到秒级的时间戳/字符串转时间戳

t6 = datetime.strptime(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S').timestamp()
print(t6, type(t6))
# 1652758879.0 <class 'float'>

time

获取当前时间字符串

import time

t1 = time.localtime()
print(t1, type(t1))
# time.struct_time(tm_year=2022, tm_mon=5, tm_mday=17, tm_hour=13, tm_min=55, tm_sec=49, tm_wday=1, tm_yday=137, tm_isdst=0) <class 'time.struct_time'>
t2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(t2, type(t2))
# 2022-05-17 13:55:49 <class 'str'>

int时间戳转时间字符串

time_stamp = 1652767169
t1 = time.localtime(time_stamp)
print(t1, type(t1))
# time.struct_time(tm_year=2022, tm_mon=5, tm_mday=17, tm_hour=13, tm_min=59, tm_sec=29, tm_wday=1, tm_yday=137, tm_isdst=0) <class 'time.struct_time'>
t2 = time.strftime("%Y.%m.%d %H:%M:%S", t1)
print(t2, type(t2))
# 2022.05.17 13:59:29 <class 'str'>

字符串转时间戳

t2 = time.mktime(time.strptime("2022-05-17 14:38:43", '%Y-%m-%d %H:%M:%S'))
print(t2, type(t2))
# 1652769523.0 <class 'float'>

获取时间戳

t3 = time.time()
print(t3, type(t3))
# 1652767464.013739 <class 'float'>

进阶

获取当天凌晨时间戳

import time
from datetime import date

t1 = int(time.mktime(date.today().timetuple()))
print(t1, type(t1))
# 1652716800 <class 'int'>

获取一分钟后的时间

t2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()) + 60))
print(t2, type(t2))
# 2022-05-17 14:41:56 <class 'str'>

获取一天前的时间

t1 = datetime.datetime.strptime("2022.05.17", "%Y.%m.%d")
print(t1, type(t1))  # 2022-05-17 00:00:00 <class 'datetime.datetime'>
t2 = datetime.timedelta(days=-1)
t3 = t1 + t2
print(t3, type(t3))  # 2022-05-16 00:00:00 <class 'datetime.datetime'>
t4 = t3.strftime("%Y.%m.%d")
print(t4, type(t4))  # 2022.05.16 <class 'str'>

♥ 喜 欢 请 点 赞 哟 ♥
(●ˇ∀ˇ●)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九思梦鹿

喜欢,请记得点赞或赞赏哟

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值