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


#6

Can we extract the year, month, etc. without storing datetime.now() in a variable?


#7

Hi @cyphered.daydream,

Yes, you can do this, but making six function calls is less efficient than making just one call, and saving the result to a variable for output …

from datetime import datetime

print '%4d/%02d/%02d %02d:%02d:%02d' % (
 datetime.now().year,
 datetime.now().month,
 datetime.now().day,
 datetime.now().hour,
 datetime.now().minute,
 datetime.now().second
)

Output …

2018/10/14 14:00:46

Edited on 2018/10/14 to consolidate the Python code for better display.


#8

I tried the following and still get the same answer.

print (current_year)

But the tutorial’s answer is print (now.year).

Which one is the correct way to write it?


#9

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.


#10

Did you see @appylpye answer:

from datetime import datetime

print '%4d/%02d/%02d %02d:%02d:%02d' % (
 datetime.now().year,
 datetime.now().month,
 datetime.now().day,
 datetime.now().hour,
 datetime.now().minute,
 datetime.now().second
)

that should answer your question


#11

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?