How to Get the Number of Days In a Month with Python


In this example, we will get an easy method of getting the number in a given month with Python calendar module.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import calendar as cal

def Day(year,month):
    temp = cal.monthrange(year, month)
    return temp

year = int(input('Please input the year: '))
month = int(input('Please inout the month: '))
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

print('For the year of {}, the {}th month has {} days and the first day of the month is {}'
.format(year, month, Day(year, month)[1], weekdays[Day(year, month)[0]]))
please input the year: 2018
please input the month: 8
For the year of 2018, the 8th month has 31 days and the first day of the month is Wednesday

please input the year:2018
please input the month: 9
For the year of 2018, the 9th month has 30 days and the first day of the month is Saturday
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments