Python program to Get Month Name from Month Number
Given a datetime object or month number, the task is to write a Python program to get the corresponding month name from it.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Input : test_date = datetime(2020, 4, 8)
Output : April
Explanation : 4th Month is April.
Input : test_date = datetime(2020, 1, 8)
Output : January
Explanation : 1st Month is January.
Method #1 : Using strftime() + %B
In this, we use strftime() which converts date object to a string using a format, and by providing %B, it’s enforced to just return a Month Name.
The original date is : 2020-04-08 00:00:00 Month Name from Date : April
Method #2: Using calendar library and month number
In this variant, rather than having datetime object as input, the month name as an input is accepted and the result is the month name returned.
The original month number is : 5 Month Name from Number : May