python 获取今天之前n天的列表

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4539

2017/11/01 01:25


python获取今天之前n天的数据列表:

>>> get_before_n_days(30)
['20171101', '20171003', '20171004', '20171005', '20171006', '20171007', '20171008', '20171009', '20171010', '20171011', '20171012', '20171013', '20171014', '20171015', '20171016', '20171017', '20171018', '20171019', '20171020', '20171021', '20171022', '20171023', '20171024', '20171025', '20171026', '20171027', '20171028', '20171029', '20171030', '20171031', '20171101']

 

def get_before_n_days(n):
    before_n_days = []
    before_n_days.append(datetime.date.today().strftime('%Y%m%d'))
    for i in range(1, n)[::-1]:
        before_n_days.append((datetime.date.today()-datetime.timedelta(days=i)).strftime('%Y%m%d'))
    before_n_days.append(datetime.date.today().strftime('%Y%m%d'))
    return before_n_days

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1