如何查找当月的天数 [英] How to find number of days in the current month
本文介绍了如何查找当月的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获得当月的天数?也就是说,如果当月是2012年1月,我应该得到31.如果是2012年2月,我应该得到29。
How can I get the number of days in the current month? I.e.,if the current month is "Jan 2012" I should get 31. If it is "Feb 2012" I should get 29.
推荐答案
如彼得所说, calendar.monthrange(年,月)
返回工作日(0-6〜周一至周日)和天数(28-31) code>年,月
。
As Peter mentioned, calendar.monthrange(year, month)
returns weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year
, month
.
>>> import calendar
>>> print calendar.monthrange(2012,1)[1]
31
>>> calendar.monthrange(2012,2)[1]
29
/ strong>:更新答案返回当前月份的天数
Edit: updated answer to return the number of days of the current month
>>> import calendar
>>> import datetime
>>> now = datetime.datetime.now()
>>> print calendar.monthrange(now.year, now.month)[1]
29
这篇关于如何查找当月的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文