Home > Blockchain >  get all dates between two dates in yyyy-mm-dd
get all dates between two dates in yyyy-mm-dd

Time:07-21

Hi I'm trying to get all dates between two dates yyyy-mm-dd, I tried this code:

def date_range(future_date):
    date_list = []
    current = datetime.strptime(datetime.today().strftime('%Y-%m-%d'), '%Y-%m-%d')
    end_dt = future_date
    for n in range(int((end_dt - current).days) 1):
        d = datetime.strftime(current   timedelta(n), '%Y-%m-%d')
        d = datetime.strptime(d, '%Y-%m-%d')
        date_list.append(d)
    return date_list
end_dt = datetime.strptime('2022-08-10', '%Y-%m-%d')
print(date_range(end_dt))

but output is not in expected form. output I get after running this code is:

datetime.datetime(2022, 7, 20, 0, 0), datetime.datetime(2022, 7, 21, 0, 0), datetime.datetime(2022, 7, 22, 0, 0), datetime.datetime(2022, 7, 23, 0, 0), datetime.datetime(2022, 7, 24, 0, 0), datetime.datetime(2022, 7, 25, 0, 0), datetime.datetime(2022, 7, 26, 0, 0), datetime.datetime(2022, 7, 27, 0, 0), datetime.datetime(2022, 7, 28, 0, 0), datetime.datetime(2022, 7, 29, 0, 0), datetime.datetime(2022, 7, 30, 0, 0), datetime.datetime(2022, 7, 31, 0, 0), datetime.datetime(2022, 8, 1, 0, 0), datetime.datetime(2022, 8, 2, 0, 0), datetime.datetime(2022, 8, 3, 0, 0), datetime.datetime(2022, 8, 4, 0, 0), datetime.datetime(2022, 8, 5, 0, 0), datetime.datetime(2022, 8, 6, 0, 0), datetime.datetime(2022, 8, 7, 0, 0), datetime.datetime(2022, 8, 8, 0, 0), datetime.datetime(2022, 8, 9, 0, 0), datetime.datetime(2022, 8, 10, 0, 0)]

Expected Output

['2022-07-20', '2022-07-21',.....,'2022-08-10']

without time. Not trying to use pandas looking for some solution related to datetime or something.

CodePudding user response:

You can use datetime.strftime(your_format).

from datetime import datetime, timedelta
def date_range(future_date):
    date_list = []
    current = datetime.today()
    end_dt = future_date
    for n in range(int((end_dt - current).days) 1):
        d = current   timedelta(n)
        date_list.append(d.strftime('%Y-%m-%d'))
    return date_list
end_dt = datetime.strptime('2022-08-10', '%Y-%m-%d')
print(date_range(end_dt))

['2022-07-20', '2022-07-21', '2022-07-22', '2022-07-23', '2022-07-24', '2022-07-25', '2022-07-26', '2022-07-27', '2022-07-28', '2022-07-29', '2022-07-30', '2022-07-31', '2022-08-01', '2022-08-02', '2022-08-03', '2022-08-04', '2022-08-05', '2022-08-06', '2022-08-07', '2022-08-08', '2022-08-09']

CodePudding user response:

If you don't want a datetime object, but want a string, remove the usage of datetime.strptime before appending to the list

CodePudding user response:

If you convert it to a string when adding it to the list it'll change the output. import datetime

def date_range(future_date):
    date_list = []
    current = datetime.datetime.strptime(datetime.datetime.today().strftime('%Y-%m-%d'), '%Y-%m-%d')
    end_dt = future_date
    for n in range(int((end_dt - current).days) 1):
        d = datetime.datetime.strftime(current   datetime.timedelta(n), '%Y-%m-%d')
        d = str(datetime.datetime.strptime(d, '%Y-%m-%d').date())
        date_list.append(d)
    return date_list

end_dt = datetime.datetime.strptime('2022-08-10', '%Y-%m-%d')
print(date_range(end_dt))

CodePudding user response:

You can use this code.

from datetime import datetime, timedelta

def date_range(start, end):
    days=[]
    delta = end - start 
    for i in range(delta.days 1):
        days.append((start timedelta(days=i)).strftime("%Y-%m-%d"))
    return days

start_date = datetime(2022, 7, 1)
end_date = datetime(2022, 7, 5)
    
print(date_range(start_date, end_date))

Output :

['2022-07-01', '2022-07-02', '2022-07-03', '2022-07-04', '2022-07-05']
  • Related