Home > Software design >  Converting Month Number to Month Name in Python
Converting Month Number to Month Name in Python

Time:06-24

I am trying to convert the current month number to the month name. I have read similar threads, but I am getting an error when I try the conventional method and I don't know what is causing the error. Here is my code:

month_name = datetime.now().month

month_name.strftime("%B")

And I am getting this error:

AttributeError                            Traceback (most recent call last)
Input In [202], in <cell line: 2>()
      1 month_name = datetime.now().month
----> 2 month_name.strftime("%B")

AttributeError: 'int' object has no attribute 'strftime'

​ How can I fix this? Thank you.

CodePudding user response:

You need to store the date corresponding to the month you are getting from datetime.now():

from datetime import datetime

date = datetime.now()
month_number = date.month

print(date.strftime("%B"), month_number)

Output:

June 6
  • Related