作者:zhattt199_117 | 来源:互联网 | 2023-10-12 16:42
字符串日期转为datetime类型importdatetimetime1994-01-1900:00:00timedatetime.datetime.strptime(tim
字符串日期转为datetime类型
import datetime
time = '1994-01-19 00:00:00'
time = datetime.datetime.strptime(time,'%Y-%m-%d %H:%M:%S')
print(time,type(time))
输出结果:
1994-01-19 00:00:00
datetime类型转为字符串类型
import datetime
time = datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d %H:%M:%S')
print(time,type(time))
输出结果:
2020-09-17 13:17:57
时间戳转为datetime类型
10位时间戳
time = datetime.datetime.utcfromtimestamp(float(1580818427))
13位时间戳
time = datetime.datetime.utcfromtimestamp(float(1580818427123)/1000)
datetime类型转为时间戳
time_ = datetime.datetime.timestamp(datetime.datetime.now())
字符串类型转为时间戳
time_ = ‘1994-01-19 00:00:00’
time_ = time.strptime(time_,’%Y-%m-%d %H:%M:%S’)
time = timestamp = time.mktime(time_)
获取当前时间(含日期)
time = datetime.datetime.now()
获取当前时间(不含日期)
time = datetime.datetime.time(datetime.datetime.now())
获取近几天的日期
day = datetime.datetime.today() – datetime.timedelta(days=7)
for i in range(7):
print(day – datetime.timedelta(days=(-i)))
王晓博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python日期格式转换