• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Month Name from Date in Python

Get Month Name from Date in Python

March 7, 2022 2 Comments

To get the month name from a number in Python, the easiest way is with strftime() and passing “%M”.

import datetime

currentDate = datetime.date.today()
currentMonthName = currentDate.strftime("%B")

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
March

You can also use the calendar module and the month_name() function.

import calendar
import datetime

currentDate = datetime.date.today()
currentMonthName = calendar.month_name[currentDate.month]

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
'March'

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to dates, sometimes we need to display a particular day or month.

We can get the name of a month from date or datetime object easily in Python using the strftime() function.

The Python strftime() function is very useful when working with date and datetime variables. strftime() accepts a string representing the format of a date and returns the date as a string in the given format.

To get the name of the month using strftime(), pass “%B”.

Below is a simple example of getting the month name from a date object in Python.

import datetime

currentDate = datetime.date.today()
currentMonthName = currentDate.strftime("%B")

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
March

If you just want the abbreviation of the month name, pass “%b” to strftime().

Below is a simple example of getting the month name abbreviation from a date object in Python.

import datetime

currentDate = datetime.date.today()
currentMonthName = currentDate.strftime("%b")

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
Mar

How to Get Name of Month Using Python calendar module

Another method to get the month name from a month number is using the calendar module. The Python calendar module has many great functions that make working with dates easy.

The calendar module has two built-in lists which have the month names and month abbreviations. These lists are “month_name” and “month_abbr”.

To get the name of given month, we can access the nth position of “month_name” and get the name of that month.

Below is a simple example of getting the month name using the Python calendar module in Python.

import calendar
import datetime

currentDate = datetime.date.today()
currentMonthName = calendar.month_name[currentDate.month]

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
'March'

If you instead want the abbreviation of the month name, then use the “month_abbr” list.

Below is a simple example of getting the month abbreviation using the Python calendar module in Python.

import calendar
import datetime

currentDate = datetime.date.today()
currentMonthName = calendar.month_abbr[currentDate.month]

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
'Mar'

How to Get Name of Month Using pandas in Python

If you are using pandas, you can get the month name from columns and Series with data type datetime.

Let’s say you have the following series of dates in pandas.

import pandas as pd

dates = pd.Series(['2022-03-05', '2020-01-31', '2019-03-02'])

To get the month names from a Series with datetimes, use dt.month_name()

Below is how to get the month name from a datetime using pandas.

import pandas as pd

dates = pd.Series(['2022-03-05', '2020-01-31', '2019-03-02'])

dates = pd.to_datetime(dates)

print(dates.dt.month_name(locale="English"))

#Output:
0      March
1    January
2      March
dtype: object

Hopefully this article has been helpful for you to learn how to get the month name in Python.

Other Articles You'll Also Like:

  • 1.  Python sinh – Find Hyperbolic Sine of Number Using math.sinh()
  • 2.  Python turtle Colors – How to Color and Fill Shapes with turtle Module
  • 3.  Get Substring Between Two Characters with Python
  • 4.  Get Random Value from Dictionary in Python
  • 5.  How to Output XLSX File from Pandas to Remote Server Using Paramiko FTP
  • 6.  Reverse Words in a String Python
  • 7.  Python Delete Variable – How to Delete Variables with del Keyword
  • 8.  Using Python to Sum Even Numbers in List
  • 9.  Time Difference in Seconds Between Datetimes in Python
  • 10.  pandas Drop Rows – Delete Rows from DataFrame with drop()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Comments

  1. john zhang says

    June 6, 2022 at 10:07 am

    “currentDate = datetime.date.today()” is not working; if remove ‘datetime.’ then fine. The parent object: datetime looks unnecessary. Not sure if my saying is correct. I use version 3.6, which might not cover the functionality.

    Reply
    • Erik says

      June 6, 2022 at 5:29 pm

      It depends on how you have imported the modules you are using. If you have:

      from datetime import date
      
      currentDate = date.today()

      then you are correct.

      Notice that in the post we are only importing datetime.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x