首先导入模块:
import datetime
import time
string转datetime:
timeStr = '2017-3-19'
date_time = datetime.datetime.strptime(timeStr,'%Y-%m-%d')
date_time
输出: datetime.datetime(2017, 3, 19, 0, 0)
datetime转string:
date_time.strftime('%Y-%m-%d')
输出: 2017-03-19
datetime转时间戳:
time_time = time.mktime(date_time.timetuple())
time_time
输出: 1489852800.0
时间戳转string:
time.strftime('%Y-%m-%d',time.localtime(time_time))
输出: 2017-03-19
date转datetime:
date = datetime.date.today()
date
datetime.date(2017,3,19)
datetime.datetime.strptime(str(date),'%Y-%m-%d') #将date转换为str,在由str转换为datetime
datetime.datetime(2017,3,19,0,0)