python时间的转化格式

参考:https://blog.csdn.net/qq_32642039/article/details/78496118

时间的三种存在方式:时间对象(datetime),时间字符串,时间戳

from datetime import datetime, timedelta
import time
 
#字符串转datetime
string = '2017-04-25 11:59:58'
time1 = datetime.strptime(string,'%Y-%m-%d %H:%M:%S')
print (time1)
print (type(time1))
#datetime转字符串
time1_str = datetime.strftime(time1, '%Y-%m-%d %H:%M:%S')
print(time1_str)
print(type(time1_str))
#时间戳转时间对象,这里用到的time模块是单独import time
new_time = time.time()
print (new_time)
now_time =datetime.fromtimestamp(new_time)

 

你可能感兴趣的:(python)