Skip to content
Advertisement

Change ‘1 Apr 2022, noon’ to datetime format python

I want to change ‘1 Apr 2022, noon’ to YYYY-MM-DD HH:MM:SS format.

I know the datetime thing in python has the ability to parse but I am unsure how

Advertisement

Answer

You can use the dateparser module’s parse() function:

>>> from dateparser import parse
>>> parse("1 Apr 2022, noon")
datetime.datetime(2022, 4, 1, 12, 0)

This gets us a datetime.datetime object. We can now call strftime() to format it properly:

>>> date_time = parse("1 Apr 2022, noon")
>>> print(date_time.strftime("%Y-%m-%d %H:%M:%S")
'2022-04-01 12:00:00'
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement