Count the number of days of a specific month using NumPy
In order to count the number of days of a specific month, we will be using numpy.datetime64() method of numpy module.
Input: month = 2; year = 2016 Output: 29 days Input: month = 12; year = 2020 Output: 31 days
Let us assume the month whose number of days we want to count be P and the next month is N. We will subtract the first date of the month P from the first date of the month N. Implementing this in numpy.datetime64() method.
For all months except December:
numpy.datetime64('yyyy-N-01') - numpy.datetime64(yyyy-P-01')
numpy.datetime64('yyyy-P-31') - numpy.datetime64(yyyy-P-01') + 1
Below are various examples which depict how to count the number of days of a specific month using numpy module:
In this example, we will find the number of days in the month of February of the year 2016.
Here, we will find the number of days in the month of April of the year 2001.
In the below program, we will find the number of days in the month of December of the year 2021.
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