Generating all dates within a given range in python

前端 未结 9 560
青春惊慌失措
青春惊慌失措 2021-02-02 08:31

I have two string variables which contain dates in yyyy-mm-dd format as follows :

date1 = \'2011-05-03\'
date2 = \'2011-05-10\'

I want to write

9条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 09:04

    import time
    
    def dates_between(start, end):
      start_epoch = int(time.mktime(time.strptime(start, "%Y-%m-%d")))
      end_epoch = int(time.mktime(time.strptime(end, "%Y-%m-%d"))) + 1 #include end
      return range(start_epoch, end_epoch, 86400)
    

提交回复
热议问题