Search code examples
pythonapirestviewcalendar

How to add current date and previous 7 days date in api automatically


I want to add startDate, last seven days of the current date, and end date as today date automatically in API in python can anyone help me?

response = requests.get('http://172.24.105.27:8092/Co=LT&StartDate=06-05-2022&EndDate=14-05-2022', headers=my_headers)
s = response.json()

Solution

  • Can you try the following:

    import datetime
    
    today = datetime.datetime.today()
    start = (today - datetime.timedelta(days=7)).strftime('%d-%m-%Y')
    end = today.strftime('%d-%m-%Y')
    
    response = requests.get(f'http://172.24.105.27:8092/Co=LT&StartDate={start}&EndDate={end}', headers=my_headers)
    s = response.json()