编程语言
首页 > 编程语言> > python各种时间的转换

python各种时间的转换

作者:互联网

import time, datetime

# 1662541290
print(int(time.time()))

t = datetime.datetime.now()
# 2022-09-07 17:01:30.954448
print(t)

# 2022-09-07 17:03:09
print(t.strftime('%Y-%m-%d %H:%M:%S'))

# 一个小时后  2022-09-07 18:04:57
t += datetime.timedelta(hours=1)
print(t.strftime('%Y-%m-%d %H:%M:%S'))

# 时间戳转固定格式时间
ts = int(time.time())
time_arr = time.localtime(ts)
time_str = time.strftime('%Y--%m--%d  %H--%M--%S')
print(time_str)

# 时间转时间搓
t2 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
time_arr2 = time.strptime(t2, '%Y-%m-%d %H:%M:%S')
times = time.mktime(time_arr2)
print(times)

 

标签:-%,各种,转换,python,09,datetime,--%,time,print
来源: https://www.cnblogs.com/fmg0224/p/16666608.html