How to convert a UTC datetime to a local datetime using only standard library?

I have a python datetime instance that was created using datetime.utcnow() and persisted in database. For display, I would like to convert the datetime instance retrieved from the database to local datetime using the default local timezone (i.e., as if the datetime was created using datetime.now()). How can I convert the UTC datetime to a … Read more

Getting today’s date in YYYY-MM-DD in Python?

Is there a nicer way than the following to return today’s date in the YYYY-MM-DD format? str(datetime.datetime.today()).split()[0] 14 s 14 Use strftime: >>> from datetime import datetime >>> datetime.today().strftime(‘%Y-%m-%d’) ‘2021-01-26’ To also include a zero-padded Hour:Minute:Second at the end: >>> datetime.today().strftime(‘%Y-%m-%d %H:%M:%S’) ‘2021-01-26 16:50:03’