python get how many days in current month 1

python get how many days in current month

import calendar
import datetime
now = datetime.datetime.now()
print calendar.monthrange(now.year, now.month)[1]
29

Here is what the above code is Doing:
1. import calendar
2. import datetime
3. now = datetime.datetime.now()
4. print calendar.monthrange(now.year, now.month)[1]

1. import calendar

This imports the calendar module.

2. import datetime

This imports the datetime module.

3. now = datetime.datetime.now()

This creates a datetime object called now.

4. print calendar.monthrange(now.year, now.month)[1]

This prints the number of days in the month of the now object.

Similar Posts