Python Snippet : Date

字串轉日期

import time
startTime='2016-08-07 11:00'
regularStartTime = time.strptime(startTime, "%Y-%m-%d %H:%M")
print regularStartTime
#>>> time.struct_time(tm_year=2016, tm_mon=8, tm_mday=7, tm_hour=11, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=220, tm_isdst=-1)

字串轉為指定格式的datetime

import datetime
startTime='2016-08-07 11:00'
regularStartTime = datetime.datetime.strptime(startTime, "%Y-%m-%d %H:%M")
print regularStartTime
#>>> 2016-08-07 11:00:00
print regularStartTime.strftime("%Y-%m-%d")
#>>> 2016-08-07

date 加法

datetime.datetime.now()
start += datetime.timedelta(days=7)