Telling the Time in Python
It’s time for you to create your first
datetime object. To begin, you’ll need to import the
datetime module in Python. You can do this by typing in the following code:
from datetime import datetime
as dt. I prefer to import the module as
However, this is not required. Next, you can instantiate a
datetime object by setting a variable name of your choice equal to
dt.now() method. For this example, I’ve named my
now, but you can name it something different if you wish.
As you can see, this output very closely aligns with using the
print() function alone. However, in this example, you can see a
T, which acts as the standard separator between the date and the time.
Become a Member to join the conversation.