发布时间:2019-09-01 10:50:54编辑:auto阅读(3222)
import datetime
def create_assist_date(datestart = None,dateend = None):
# 创建日期辅助表
if datestart is None:
datestart = '2016-01-01'
if dateend is None:
dateend = datetime.datetime.now().strftime('%Y-%m-%d')
# 转为日期格式
datestart=datetime.datetime.strptime(datestart,'%Y-%m-%d')
dateend=datetime.datetime.strptime(dateend,'%Y-%m-%d')
date_list = []
date_list.append(datestart.strftime('%Y-%m-%d'))
while datestart<dateend:
# 日期叠加一天
datestart+=datetime.timedelta(days=+1)
# 日期转字符串存入列表
date_list.append(datestart.strftime('%Y-%m-%d'))
print(date_list)
if __name__ == '__main__':
create_assist_date("2018-06-28")
['2018-06-28', '2018-06-29', '2018-06-30', '2018-07-01', '2018-07-02', '2018-07-03', '2018-07-04', '2018-07-05', '2018-07-06']
import datetime
def get_nday_list(n):
before_n_days = []
for i in range(1, n + 1)[::-1]:
before_n_days.append(str(datetime.date.today() - datetime.timedelta(days=i)))
return before_n_days
a = get_nday_list(7)
print(a)
['2017-02-22', '2017-02-23', '2017-02-24', '2017-02-25', '2017-02-26', '2017-02-27', '2017-02-28']
import datetime
def get_day_nday_ago(date,n):
t = time.strptime(date, "%Y-%m-%d")
y, m, d = t[0:3]
Date = str(datetime.datetime(y, m, d) - datetime.timedelta(n)).split()
return Date[0]
# 示例
a=get_day_nday_ago('2017-02-11',7)
print a
2017-02-04
上一篇: Python常用库汇总
下一篇: LINUX CentOS下安装pytho
47869
46434
37322
34767
29337
25999
24953
19970
19569
18057
5812°
6436°
5953°
5979°
7084°
5929°
5970°
6463°
6428°
7804°