The output of converted date is ‘20210514’
input_date = '14-05-2021' # 14th May 2021
converted_date = (pd.to_datetime(input_date).strftime('%Y%m%d'))
But if I use the same logic below, why is it coming up like ‘20220601’ (1st June 2022)? The output I expect is ‘20220106’.
input_date = '06-01-2022' # 6th Jan 2022
converted_date = (pd.to_datetime(input_date).strftime('%Y%m%d'))
>Solution :
You haven’t provided a format for the input date string so pandas has to guess at it. 06-01-2022 is ambiguous: it could be month-day-year or day-month-year.
pandas.to_datetime() has a dayfirst argument that defaults to false. Since 06 is a valid month, it is being parsed as a month. 14 is not a valid month, so pandas figures out it should be the day.
Try this instead:
input_date = '06-01-2022' # 6th Jan 2022
converted_date = (pd.to_datetime(input_date, dayfirst=True).strftime('%Y%m%d'))
You can also pass in a format if you want to be really specific.