关于python:python-创建日期范围

38次阅读

共计 599 个字符,预计需要花费 2 分钟才能阅读完成。

def mkurl(self):
        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'))

            return [i.replace('-', '') for i in date_list]

        for i in create_assist_date("2018-12-31", "2020-01-01"):
            yield self.listurl.format(i)

正文完
 0