Can I display the month name instead of a number?


Can I display the month name instead of a number?


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 =

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.


When I type

now =
print now

I get

2018-08-16 13:00:42.874437

But when I type

print now.second

i get


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


if you check the documentation:

you see that you can use microsecond to get this information


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

now =
now_year= now.year


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):

    def now(cls):
        cls.year = 2018
        return datetime()

time =
print time.year

maybe not an entirely accurate representation, but it gives you an idea (hopefully), otherwise come back to this later

1 Like

Can we extract the year, month, etc. without storing in a variable?

1 Like

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' % (,,,,,

Output …

2018/10/14 14:00:46

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


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?

1 Like

You don’t have to store now.year, now.month, and in a variable, right? I’m asking because in the codeacademy example, each one is stored in a variable.

1 Like

Did you see @appylpye answer:

from datetime import datetime

print '%4d/%02d/%02d %02d:%02d:%02d' % (,,,,,

that should answer your question


While it is possible to assemble a date and time from multiple calls to, it might not be a good idea to do so. Consider, for example, what would happen if was executed just a tiny fraction of a second before midnight prior to the end of December 31, and 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?


How can I display dd-mm-yy ?
print ‘%02d-%02d-%02d’ % (, now.month, now.year)
It gives same output 21-06-2019

Conventional string formatting with “%02d” doesn’t know what you want to do with a four-digit number.

You’ll need to use the obj.strftime() method:

from datetime import datetime
now  =

print("%02d-%02d-%02d" % (, now.month, now.year))


1 Like

Why the times that appear when I print is one hour less?

maybe that’s the time where your code runs (assuming they care about their location, they might also simply be set to GMT+0 or something like that)

And how I do to actually see my real date and time?

by asking your own computer as opposed to one somewhere on the other end of your network cable

1 Like

what to do if we want to print the date also?

Can I display miler seconds?

if u check the ex, we are printing date using