Yes! By default we’re accessing the number value of the current month, day, year, minute, hour, and second of the day. We can customize a lot of how we display this.
We format the output using strftime(), which won’t be discussed on Codecademy, but it’s a cool tool to have! For a full understanding of how to do it, take a look at the documentation for this method.
Take a look at the code below:
from datetime import datetime
now = datetime.now()
print(now.strftime('%B')) # Displays the current month’s name
The code %B tells strftime() that we want to display the month as its full name, and there are lots of other codes ready for use in the link above.
because .now() is a method, while year is an instance variable. But to fully grasps this concept you need to understand classes. Regardless, here is an example:
class datetime(object):
@classmethod
def now(cls):
cls.year = 2018
return datetime()
time = datetime.now()
print time.year
maybe not an entirely accurate representation, but it gives you an idea (hopefully), otherwise come back to this later
You don’t have to store now.year, now.month, and now.day in a variable, right? I’m asking because in the codeacademy example, each one is stored in a variable.
While it is possible to assemble a date and time from multiple calls to datetime.now(), it might not be a good idea to do so. Consider, for example, what would happen if datetime.now().year was executed just a tiny fraction of a second before midnight prior to the end of December 31, and datetime.now().month was executed just a tiny fraction of a second after midnight. By how much time would we be off when all the components of the date and time were assembled and displayed?