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:
JavaScript
x
4
1
>>> from dateparser import parse
2
>>> parse("1 Apr 2022, noon")
3
datetime.datetime(2022, 4, 1, 12, 0)
4
This gets us a datetime.datetime
object. We can now call strftime()
to format it properly:
JavaScript
1
4
1
>>> date_time = parse("1 Apr 2022, noon")
2
>>> print(date_time.strftime("%Y-%m-%d %H:%M:%S")
3
'2022-04-01 12:00:00'
4