Python Format datetime

Jul 2, 2019

Convert datetime to string

import datetimenow = datetime.datetime.now()now_str = str(now))
2019-07-02 20:00:27.230410

Format yyyy-MM-dd HH:mm:ss

now_str = now.strftime('%Y-%m-%d %H:%M:%S')
'2019-07-02 20:00:27'

NOTE: Refer strftime format directives.

Format ISO

now_str = now.isoformat()
2019-07-02T20:00:27.230410

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.