To get the last date in a specific month?

Asked 2 years ago, Updated 2 years ago, 87 views

Is there a way to find out how many days are in a particular month using Python standard library? It would be nice if it was a standard library, but if it doesn't have that function, other packages related to date are fine.

python date

2022-09-22 12:27

1 Answers

calendar.monthrange(year, month) has this feature.

calendar.monthrange(year, month) returns the tuple, and this tuple's

Therefore, to find out the total number of days, you can use it with calendar.monthrange(year, month)[1].

import calendar

print calendar.monthrange(2016,1) #(4,31)
print calendar.monthrange(2016,4) #(4,30)

print calendar.monthrange(2016,1)[1] #31
print calendar.monthrange(2016,4)[1] #30


2022-09-22 12:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.