以下全部引入
form datetime import datetime, timedelta
import time
一、time 转str
二、datetime 转 str
str_date = datetime.now().strftime("%Y-%m-%d") -------(%04d%02d%02d)此种格式化注意
三、str 转 datetime
start_date = datetime.strptime("2016-06-07", "%Y-%m-%d")
四、实例应用
def datelist(start, end):
start_date = datetime.strptime(start, "%Y-%m-%d")
end_date = datetime.strptime(end, "%Y-%m-%d")
result = []
curr_date = start_date
while curr_date != end_date:
# result.append("%04d-%02d-%02d" % (curr_date.year, curr_date.month, curr_date.day))
result.append(curr_date.strftime("%Y-%m-%d"))
curr_date += timedelta(1)
# result.append("%04d-%02d-%02d" % (curr_date.year, curr_date.month, curr_date.day))
result.append(curr_date.strftime("%Y-%m-%d"))
return result
if __name__ == "__main__":
print datelist("2016-06-07", "2016-06-27")
----------2016-6-29 15:51:03--
source: 【1】python string to datetime datetime to string