How to get the last day of the month?

后端 未结 30 2594
迷失自我
迷失自我 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 06:50

    For me it's the simplest way:

    selected_date = date(some_year, some_month, some_day)
    
    if selected_date.month == 12: # December
         last_day_selected_month = date(selected_date.year, selected_date.month, 31)
    else:
         last_day_selected_month = date(selected_date.year, selected_date.month + 1, 1) - timedelta(days=1)
    

提交回复
热议问题