import datetime
import time
str = ‘2012-11-19‘
date_time = datetime.datetime.strptime(str,‘%Y-%m-%d‘)
date_time
datetime.datetime(2012,11,19,0,0)
date_time.strftime(‘%Y-%m-%d‘)
‘2012-11-19‘
time_time = time.mktime(date_time.timetuple())
time_time
1353254400.0
time.strftime(‘%Y-%m-%d‘,time.localtime(time_time))
‘2012-11-19‘
date = datetime.date.today()
date
datetime.date(2012,11,19)
datetime.datetime.strptime(str(date),‘%Y-%m-%d‘) #将date转换为str,在由str转换为datetime
datetime.datetime(2012,11,19,0,0)
本文出自 “大道至简” 博客,请务必保留此出处http://1161192890.blog.51cto.com/2041190/1638307
python中date、datetime、string的相互转换
原文地址:http://1161192890.blog.51cto.com/2041190/1638307