Can I display the month name instead of a number?

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.

12 Likes

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?

2 Likes

if you check the documentation:

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

you see that you can use microsecond to get this information

2 Likes

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

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

2 Likes

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

1 Like

Can we extract the year, month, etc. without storing datetime.now() 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' % (
 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.

5 Likes

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 now.day 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' % (
 datetime.now().year,
 datetime.now().month,
 datetime.now().day,
 datetime.now().hour,
 datetime.now().minute,
 datetime.now().second
)

that should answer your question

3 Likes

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?

2 Likes

How can I display dd-mm-yy ?
print ‘%02d-%02d-%02d’ % (now.day, 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  = datetime.now()

print("%02d-%02d-%02d" % (now.day, now.month, now.year))
print(now.strftime("%d-%m-%y"))

Output:

21-06-2019
21-06-19
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 now.day