Kodeclik Blog
Finding the current month in Python
There are many situations where inside your Python program you will need to find the current month. This is useful across many domains.
For instance, in data organization and filing, the current month is essential for creating automatic folder structures, generating monthly reports, and organizing financial records. In business contexts, particularly in accounting and finance, knowing the current month is crucial for calculating monthly revenue, processing payroll, and generating financial statements. Many businesses operate on monthly cycles, so to keep track of happenings in the business you will need to determine what the current month is and ensure that all transactions for that month are properly accounted for.
Let us explore some Python programs to find the current month. Here are three ways to do so!
Method 1: Use datetime.now()
Here is how this works:
from datetime import datetime
current_month = datetime.now().strftime("%B")
print(current_month)
This method uses the datetime module's now() function to get the current date and time, then formats it using strftime() with the %B format code to get the full month name. The strftime() function converts the date object to a string based on specified format codes, where %B returns the full month name. If we run this program, we will get:
November
(or whatever month is current in your timezone.)
Method 2: Use calendar module
The second approach uses a different module, viz. the calendar module.
import calendar
from datetime import datetime
current_month_num = datetime.now().month
current_month = calendar.month_name[current_month_num]
print(current_month)
This approach uses the calendar module which provides a month_name list containing all month names. We first get the current month number using datetime.now().month, then use that number as an index into calendar.month_name to get the corresponding month name. The output is again:
November
Method 3: Use date.today()
Here is the third approach:
from datetime import date
current_month = date.today().strftime("%B")
print(current_month)
This method uses the date class's today() function which returns the current local date. Like the first method, it uses strftime() with %B to format the month as a full name, but date.today() is slightly more efficient than datetime.now() when you only need the date portion.
The output is again:
November
Sometimes you might wish to obtain the abbreviated month name (like "Nov" instead of "November") in Python. Again there are three ways!
Finding the abbreviated month name using the calendar module
Here we have something similar to the calendar module usage earlier but we use a different array to map month numbers to abbreviations.
import calendar
current_month_num = datetime.now().month
month_abbr = calendar.month_abbr[current_month_num]
print(month_abbr) # Output: Nov
This method uses calendar.month_abbr instead of calendar.month_name to get the abbreviated version of the month name.
Find the abbreviated month name using datetime with strftime()
This approach uses a different format string to obtain the abbreviated versions of the month names.
from datetime import datetime
month_abbr = datetime.now().strftime("%b")
print(month_abbr) # Output: Nov
This approach uses the %b format code instead of %B in strftime() to get the abbreviated month name. The %b formatter specifically returns the locale's abbreviated month name.
Finding the abbreviated month name using date.today()
This is again similar to the earlier date.today() approach but uses a different format string to obtain the abbreviated month name.
from datetime import date
month_abbr = date.today().strftime("%b")
print(month_abbr) # Output: Nov
This is thus similar to the datetime method, but uses date.today() instead. This approach is slightly more efficient when you only need the date portion. The %b formatter will return the three-letter abbreviation of the month.
All of the above are minor variations of the programs we saw earlier.
As you can see from the above, there are very expressive modules and very handy functions available for us to obtain the current month in python by name.
Exploring a calendar using month names
Here's an elegant program that prints the current month by name and then continues to print the names of the next 11 months. We will use datetime() to illustrate the idea.
from datetime import datetime, timedelta
# Get current month name
current_month = datetime.now().strftime("%B")
print(f"Starting from: {current_month}")
print("\nNext 12 months:")
# Get current month number (1-12)
current_month_num = datetime.now().month
# Create list of all month names
months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"]
# Print current month and next 11 months
for i in range(12):
month_index = (current_month_num - 1 + i) % 12
print(f"{i+1}. {months[month_index]}")
This program first displays the current month using strftime() with the %B format code. Then, it creates a list of all month names and uses modulo arithmetic to cycle through the months starting from the current one. The modulo operator (%) ensures that when we reach December, we wrap back to January correctly. The program numbers each month in the output for better readability.
The output will be:
Starting from: November
Next 12 months:
1. November
2. December
3. January
4. February
5. March
6. April
7. May
8. June
9. July
10. August
11. September
12. October
Such an output is particularly useful for planning ahead or creating schedules where the “first month” is not quite January.
Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.