Last active
March 26, 2021 20:54
-
-
Save blaylockbk/ab3a74504a8c46b29d290b26d3977969 to your computer and use it in GitHub Desktop.
Methods to format python datetime object as strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
DATE = datetime(2019, 1, 1, 12, 35) | |
print(DATE) | |
# The most common way, with formatters | |
# https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior | |
d1 = DATE.strftime('%Y %b %d, %H:%M') | |
print('The date and time is %s' % d1) | |
# A very convient way, inline with the text (for Python v3.6+) | |
# https://realpython.com/python-f-strings/ | |
# https://www.blog.pythonlibrary.org/2018/03/13/python-3-an-intro-to-f-strings/ | |
#https://www.python.org/dev/peps/pep-0498/ | |
print(f"It's currently: {DATE:%Y-%m-%d %H:%M}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment