gpt4 book ai didi

python - 我们如何在python中确定给定月份的天数

转载 作者:IT老高 更新时间:2023-10-28 21:05:54 25 4
gpt4 key购买 nike

我需要在 python 中计算给定月份的天数。如果用户输入 2011 年 2 月,程序应该能够告诉我 2011 年 2 月有 28 天。谁能告诉我应该使用哪个库来确定给定月份的长度。

最佳答案

您应该使用 calendar.monthrange :

>>> from calendar import monthrange
>>> monthrange(2011, 2)
(1, 28)

明确一点,monthrange 也支持闰年:

>>> from calendar import monthrange
>>> monthrange(2012, 2)
(2, 29)

正如@mikhail-pyrev 在评论中提到的那样:

First number is the weekday of the first day of the month, the second number is the number of days in said month.

关于python - 我们如何在python中确定给定月份的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4938429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com