0

I am creating desktop application using python i want to convert any incoming sting date format in yyyymmdd format i am using following code to convert date in desired format but this is not working.

anydate = "09-12-2020 12:00:00"
parsedate = dateutil.parser.parse(anydate)
x = parsedate.strftime('%Y-%m-%d %H:%M:%S')
print(x)

when i am converting date from 1 to 11 then it convert it into YYYYDDMM format and when i am converting date after 11 then it convert into YYYYMMDD format please help me if anyone have idea.

Thanks in advance.

siya
  • 43
  • 8
  • The problem is not with the `parsedate.strftime` conversion. The problem is that `dateutil.parser.parse(anydate)` has to **guess** the format of `anydate`. Of course there is an ambiguity. If I tell you my birthday is on "7/8", how could you know whether I was born on the 7th of august or on the 8th of july? If you already know in which order is the input date string (day/month or month/day) then you can supply that information as an optional argument to `dateutil.parser.parse`; See the doc: https://dateutil.readthedocs.io/en/stable/parser.html – Stef Dec 10 '20 at 13:42
  • "*any incoming sting date format*" - how do you know if the input is month-day or day-month (assuming both numbers < 13)? dateutil.parser.parse will *assume* month-first by default - see [docs](https://dateutil.readthedocs.io/en/stable/parser.html), kwargs. – FObersteiner Dec 10 '20 at 13:43

0 Answers0