How to get the last day of the month?

后端 未结 30 3234
迷失自我
迷失自我 2020-11-22 06:13

Is there a way using Python\'s standard library to easily determine (i.e. one function call) the last day of a given month?

If the standard library doesn\'t support

30条回答
  •  半阙折子戏
    2020-11-22 07:04

    If you want to make your own small function, this is a good starting point:

    def eomday(year, month):
        """returns the number of days in a given month"""
        days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        d = days_per_month[month - 1]
        if month == 2 and (year % 4 == 0 and year % 100 != 0 or year % 400 == 0):
            d = 29
        return d
    

    For this you have to know the rules for the leap years:

    • every fourth year
    • with the exception of every 100 year
    • but again every 400 years

提交回复
热议问题