Generating all dates within a given range in python

前端 未结 9 552
青春惊慌失措
青春惊慌失措 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 08:59

    Assuming your dates are already as a datetime.date class you can use .fromordinal and .toordinal to create this oneliner.

    from datetime import date
    
    start_date = date(2011, 5, 3)
    end_date = date(2011, 5, 10)
    
    [date.fromordinal(i) for i in range(start_date.toordinal(), end_date.toordinal())]
    

    The result is exclusive end_date. Use end_date.toordinal() + 1 for a range inclusive end_date.

    0 讨论(0)
  • 2021-02-02 09:02

    I like this one because it is intuitive and it gives an array of date strings.

    import re
    import datetime
    
    def datetime_to_str_date(dt):
        return re.sub(r'\T.+$','', dt.isoformat())
    
    start_date = datetime.datetime.strptime('2016-01-01', '%Y-%m-%d')
    end_date = datetime.datetime.today()
    num_of_days = (end_date - start_date).days
    
    date_list = map(
            datetime_to_str_date, 
            [start_date + datetime.timedelta(days=x) for x in range(0, num_of_days)]
    )
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-02-02 09:08
    import pandas as pd
    date1 = '2011-05-03'
    date2 = '2011-05-10'
    pd.date_range(start = date1,end = date2)
    
    0 讨论(0)
  • 2021-02-02 09:12

    Dates can be compared to each other just like numbers, and you can do date-related math with the datetime.timedelta object. There's no reason to use dateutil here, and there's no reason to hard-code the number of iterations a la 'range(9)'. This really becomes similar to how you'd deal with plain old numbers.

    >>> import datetime
    >>> date1 = '2011-05-03'
    >>> date2 = '2011-05-10'
    >>> start = datetime.datetime.strptime(date1, '%Y-%m-%d')
    >>> end = datetime.datetime.strptime(date2, '%Y-%m-%d')
    >>> step = datetime.timedelta(days=1)
    >>> while start <= end:
    ...     print start.date()
    ...     start += step
    ... 
    2011-05-03
    2011-05-04
    2011-05-05
    2011-05-06
    2011-05-07
    2011-05-08
    2011-05-09
    2011-05-10
    >>> 
    
    0 讨论(0)
  • 2021-02-02 09:15
    >>> for a in range(9):
    ...     print(datetime.date(2011, 05, 03) + datetime.timedelta(a))
    ...
    2011-05-03
    2011-05-04
    2011-05-05
    2011-05-06
    2011-05-07
    2011-05-08
    2011-05-09
    2011-05-10
    2011-05-11
    

    I'm not too sure whether the parsing of the strings was integral or just the way you started the question. If so, please disregard the answer as oversimplified

    0 讨论(0)
提交回复
热议问题