0

Is there any way to print the name of the incremented month?

import datetime
currentDT = datetime.datetime.now()
currentmonth=currentDT.strftime("%B")
nextmonth = datetime.date.today().month + 1

Here I get the "nextmonth" in form of integer.

7

But how can I get the name of that month i.e. July. I even tried using this,

next_month=nextmonth.strftime("%B")

But it returned the error,

AttributeError: 'int' object has no attribute 'strftime'
DaSnipeKid
  • 31
  • 6

4 Answers4

1

You can use calendar module

import calendar
print (calendar.month_name[4])

It is built-in.

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
Sakib Farhad
  • 120
  • 1
  • 7
0

For edge cases like December as calendar.month_name has 13 elements

import calendar
import datetime
months=calendar.month_name[1:]
a=months[datetime.date.today().month % 12]
print(a)
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
-1

Your problem is that nextmonth is an int representing month number, not date object, so you can use calendar.month_name array like

>>> import calendar
>>> calendar.month_name[(datetime.date.today().month + 1) % 12]
'July'
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
-2

You can do it like this in python 3

import time,datetime

currentDT = datetime.datetime.now()
currentmonth=currentDT.strftime("%B")
nextmonth = datetime.date.today().month + 1
print(time.strftime('%B', time.struct_time((0, nextmonth, 0,)+(0,)*6)))
piet.t
  • 11,718
  • 21
  • 43
  • 52
Rajan Sharma
  • 2,211
  • 3
  • 21
  • 33
  • Your code at `print(time.strftime('%B', time.struct_time((0, nextmonth, 0,)+(0,)*6)))` raise an exception: `Exception has occurred: ValueError strftime() requires year in [1; 9999]`. Do you mean: `print(time.strftime('%B', time.struct_time((1, nextmonth, 0,)+(0,)*6)))` ? – Carlo Zanocco Sep 28 '20 at 16:35