Can I display the month name instead of a number?


#1

Question

Can I display the month name instead of a number?

Answer

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.


FAQ: Learn Python -Date and Time- Extracting Information
#2

When I type

now = datetime.now()
print now

I get

2018-08-16 13:00:42.874437

But when I type

print now.second

i get

42

How can i get the digits after the decimal in the seconds field?


#3

if you check the documentation:

https://docs.python.org/2/library/datetime.html

you see that you can use microsecond to get this information


#4

Why do we use brackets in the first example but not the second?

now = datetime.now()
now_year= now.year


#5

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