非函数 :python遍历两个日期之间的时间
import datetime
end = '2016-06-01'
start = '2019-07-12'
datestart = datetime.datetime.strptime(start, '%Y-%m-%d')
dateend = datetime.datetime.strptime(end, '%Y-%m-%d')
while datestart > dateend:
datestart -= datetime.timedelta(days=1)
print (datestart.strftime('%Y%m%d'))
url_time = datestart.strftime('%Y%m%d')
print(type(url_time))
image.png
image.png
函数方式
date_sq_list = []
def sqday(begin_date, end_date):
begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d")
while begin_date <= end_date:
date_str = begin_date.strftime("%Y-%m-%d")
date_sq_list.append(date_str)
begin_date += datetime.timedelta(days=1)
return date_sq_list
# print(sqday('1985-01-01', '2019-07-21'))
# print(date_sq_list,type(date_sq_list[0]))
时间减去一年、一个月
#pip install python-dateutil
import datetime
import dateutil.relativedelta
# 时间减去一个月
def date_minus_month(gk_date):
d = datetime.datetime.strptime(gk_date, "%Y-%m-%d") # 2013-02-28 00:00:00
date_mm = d - dateutil.relativedelta.relativedelta(months=1)
date_mm = datetime.datetime.strftime(date_mm, "%Y-%m-%d")
return date_mm
# 时间减去一年
def date_minus_year(gk_date):
d = datetime.datetime.strptime(gk_date, "%Y-%m-%d") # 2013-02-28 00:00:00
date_mm = d - dateutil.relativedelta.relativedelta(years=2)
date_mm = datetime.datetime.strftime(date_mm, "%Y-%m-%d")
print(date_mm)
return date_mm
网友评论