python-->date、datetime、string相互转换
Python--常用时间类型格式之间的转换
import datetime
import time
# 1.string转datetime
>>> str = '2012-11-19'
>>> date_time = datetime.datetime.strptime(str,'%Y-%m-%d')
>>> date_time
result: datetime.datetime(2012,11,19,0,0)
# 2.datetime转string
>>>date_time.strftime('%Y-%m-%d')
result: '2012-11-19'
# 3.datetime转时间戳
>>> time_time = time.mktime(date_time.timetuple())
>>> time_time
result: 1353254400.0
# 4.时间戳转string
>>>time.strftime('%Y-%m-%d',time.localtime(time_time))
result: '2012-11-19'
# 5.date转datetime
>>> date = datetime.date.today()
>>> date
result: datetime.date(2012,11,19)
# 6.将date转换为str,在由str转换为datetime
>>> datetime.datetime.strptime(str(date),'%Y-%m-%d')
result: datetime.datetime(2012,11,19,0,0)
参考原文:https://www.cnblogs.com/cathouse/archive/2012/11/19/2777678.html