当前位置:首页python > 正文

python获取当月最后一天

作者:野牛程序员:2023-07-26 10:18:52python阅读 2628

在Python中,可以使用calendar模块来获取当月的最后一天。以下是获取当月最后一天的示例代码:

import calendar
import datetime

def get_last_day_of_month(year, month):
    # 获取当月的日历
    _, last_day = calendar.monthrange(year, month)
    return last_day

# 获取当前日期
current_date = datetime.date.today()

# 获取当月的年份和月份
current_year = current_date.year
current_month = current_date.month

# 获取当月的最后一天
last_day_of_month = get_last_day_of_month(current_year, current_month)

print(f"The last day of the current month is: {last_day_of_month}")

这段代码会输出类似于:"The last day of the current month is: 31" 这样的结果,具体根据当月实际天数而定。


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击