Find the number of months remaining in a year in Python
In this tutorial, we are going to learn how to find the number of months remaining in a year in Python, to find this we required datetime package.
Find remaining month using datetime module in Python
First, we have to import datetime module.
To get today’s date we have to use date.today() function.
Here we use curr_date variable to store the current date.
curr_date = datetime.date.today()
To get the current month we have to use the month function.
Here we will store the current month number in mo_n variable.
To find the number of the month remaining we have to subtract mo_n from 12,
and we store the remaining month in re_mon variable.
re_mon = 12 - mo_n
import datetime curr_date = datetime.date.today() mo_n= curr_date.month re_mon = 12 - mo_n print("number of months remaining in year is :",re_mon)
number of months remaining in year is : 9
Also read: Python Date and Time