整个的思路比较简单:
import calendar
from datetime import datetime
def get_last_day(year: int, month: int):
last_day = calendar.monthrange(year, month)[-1] # 本月的最后一天
return datetime(year, month, last_day)
print(get_last_day(2023, 1))
得到结果:
2023-01-31 00:00:00