0

I've been trying to add one day to the following string: '01/05/2020' The code below works, but it outputs with the hour information as well, which is not what I'm looking for. I'm looking for a simple dd/mm/yyyy output. Do you know what I need to change to get to that?

import datetime
import pandas as pd

day = '01/05/2020'
date = pd.to_datetime(day, format='%d/%m/%Y')
print((date + datetime.timedelta(days=1))

Output:

02/05/2020  00:00:00

Desired Output:

02/05/2020

Thanks in advance!

MTavares
  • 27
  • 7
  • 1
    print((date + datetime.timedelta(days=1)).strftime("%d/%m/%Y")) – Arjen Dijkstra May 02 '20 at 13:38
  • https://stackoverflow.com/questions/16176996/keep-only-date-part-when-using-pandas-to-datetime maybe this will help you ! – PLF May 02 '20 at 13:39
  • if you just want to print the date, `print((date + datetime.timedelta(days=1)).date())` - if you want to work with date and time, have a look at the difference between strings and datetime objects ;-) – FObersteiner May 02 '20 at 13:44

2 Answers2

0

Please try the the below code. date() function will give you the desired result.

import datetime
import pandas as pd

day = '01/05/2020'
date = pd.to_datetime(day, format='%d/%m/%Y')
print((date + datetime.timedelta(days=1)).date()) 

Shibu Tewar
  • 136
  • 1
  • 5
  • @MTavares I am glad it worked, could you please mark it as answered and upvote the answer if possible, so that community members know that this has been answered, Thank you. – Shibu Tewar May 02 '20 at 18:14
0

In pd.to_datetime() method format='%d/%m/%Y' specifies the input format. The output format will always be a Timestamp if a scalar is passed.

You can then convert the output to string using strftime() method as follows:

from datetime import datetime, timedelta
import pandas as pd

day = '01/05/2020'
date = pd.to_datetime(day)
print((date + timedelta(days=1)).strftime("%d/%m/%y"))

Or you can use .date() method to output date only.