pythondatetimedate-formattingtimedelta

How to change date format and add a day to the date


I have a date format that looks like this: 2016-08-30 and need to transform it to yyyy,mm,dd, as well as add a day. The purpose is to get a quandl format for days.

i = datetime.strptime(i,'%Y-%m-%d').strftime('%Y,%-m,%d')
print i
j = dt.datetime(i) + dt.timedelta(1)

This is the code I have. It tells me that I need to have an int in the dt.datetime function, but when I make it an int by doing dt.datetime(int(i)), I get this error.

ValueError: invalid literal for int() with base 10: '2016,8,30'

Thanks


Solution

  • You can use timedelta to add a day and use strftime to format the date string.

    import datetime
    
    dt = datetime.datetime.strptime('2016-01-23', '%Y-%m-%d')
    dt = dt + datetime.timedelta(days=1)
    print dt.strftime('%Y,%m,%d')