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
STEP 1:
First, we have to import datetime module.
import datetime
STEP 2:
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()
STEP 3:
To get the current month we have to use the month function.
Here we will store the current month number in mo_n variable.
mo_n= today.month
STEP 4:
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
EXAMPLE CODE:-
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)
OUTPUT:-
number of months remaining in year is : 9
Also read: Python Date and Time
How to remove all the special characters from a text file in Python
Leave a Reply