python时间转换


>>> import datetime

string转datetime

>>> import datetime
>>> str = "2016-04-03"
>>> date_time = datetime.datetime.strptime(str,'%Y-%m-%d')
>>> date_time
datetime.datetime(2016, 4, 3, 0, 0)

datetime转string

>>> date_time.strftime('%Y-%m-%d')
'2016-04-03'

datetime转时间戳

>>> time_time = time.mktime(date_time.timetuple())
>>> time_time
1459612800.0

时间戳转string

>>> time.strftime('%Y-%m-%d',time.localtime(time_time))
'2016-04-03'