python中获取某个月有多少天的方法
先说结论:
- python3.7以上,calendar中有monthlen这个方法,用起来更方便。
- python3.6没有这个方法,使用calendar.monthrange这个方法吧。
用法说明
>>> import calendar
>>> calendar.monthrange(2010,10)[1]
31
>>> calendar.monthrange(2020,3)
(6, 31)
其实这种方法随便一搜一大把,我看到的全都是使用calendar.monthrange这个方法,返回一个元祖,第一个值代表该月第一天是周几,第二个参数代表该月有多少天。
>>> import calendar,datetime
>>> calendar.monthlen(2020,2)
29
>>> calendar.monthlen(2020,3)
31
>>> today = datetime.date.today()
>>> calendar.monthlen(today.year,today.month)
31
打开calendar.py源码,发现其中还有monthlen这个方法更适用一些,效果如上。
calendar.py中的源码
def monthrange(year, month):"""Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) foryear, month."""if not 1 <= month <= 12:raise IllegalMonthError(month)day1 = weekday(year, month, 1)ndays = mdays[month] + (month == February and isleap(year))return day1, ndaysdef monthlen(year, month):return mdays[month] + (month == February and isleap(year))
另外只使用datetime模块的方法:
>>> import datetime
>>> (datetime.date(2020,3,1) - datetime.date(2020,2,1)).days
29
>>> today = datetime.date.today()
>>> ((today.replace(day=28) + datetime.timedelta(days=4)).replace(day=1)-datetime.timedelta(days=1)).day
31
# 先计算出下个月的第一天,再减一天,取得该月的最后一天,最后取出day
python版本问题
python3.6中的效果,没有monthlen方法
zang@zang-PC:~$ python3.6
>>> import calendar
>>> calendar.monthlen(2020,3)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
python3.7中的效果
zang@zang-PC:~$ python3.7
>>> import calendar
>>> calendar.monthlen(2020,3)
31
python中获取某个月有多少天的方法相关推荐
- c#获取某月有多少天的方法
//第一种 public int GetDay1() { DateTime dt = DateTime.Now; int ...
- python获取一个月有多少天
可以使用 Python 的 calendar 模块来获取一个月有多少天. 首先,需要导入 calendar 模块: import calendar 然后,可以使用 calendar.monthrang ...
- Python中获取异常(try Exception)信息
Python中获取异常(try Exception)信息 参考文章: (1)Python中获取异常(try Exception)信息 (2)https://www.cnblogs.com/hixiao ...
- 在Python中获取文件大小? [重复]
本文翻译自:Getting file size in Python? [duplicate] This question already has an answer here: 这个问题在这里已有答案 ...
- python金融数据怎么获取_class类怎样在python中获取金融数据?
我们搜集金融数据,通常想要的是利用爬虫的方法.其实我们最近所学的class不仅可以进行类调用,在获取数据方面同样是可行的,很多小伙伴都比较关注理财方面的情况,对金融数据的需要也是比较多的.下面就cla ...
- python查看系统进程_在Python中获取操作系统的进程信息
本文主要介绍在 Python 中使用 psutil 获取系统的进程信息. 1 概述 psutil 是 Python 的一个进程和系统工具集模块,通过使用 psutil,我们可以在 Python 中获取 ...
- 如何在Python中获取文件创建和修改日期/时间?
我有一个脚本,该脚本需要根据文件创建和修改日期执行一些操作,但必须在Linux和Windows上运行. 在Python中获取文件创建和修改日期/时间的最佳跨平台方法是什么? #1楼 最好的功能是os. ...
- python中获取文件大小_如何在Python中获取文件大小
python中获取文件大小 We can get file size in Python using the os module. 我们可以使用os模块在Python中获取文件大小. Python中的 ...
- 如何获取某个月有多少天
// 获取当前月有多少天getDate(date) {let d = new Date(date);// 将日期设置为下月一号d.setMonth(d.getMonth() + 1);d.setDat ...
最新文章
- python哪一版好用-Python最好用的编辑器是哪款?北京老男孩教育
- Win2003和Win2008防火墙导致FTP服务器不能访问的解决方法
- 对于一颗具有n个结点,度为4的树来说,( )
- Unity基础之:UnityAPI的学习
- 存储管理的页面置换算法
- 理论基础 —— 排序 —— 直接插入排序
- PYTOHN1.day14
- php图像处理缩略图,17.ThinkPHP 扩展库:图像处理--生成缩略图
- hung task日志linux,Linux Kernel Crash--hung_task_timeout_secs
- 快速计算Distinct Count
- 数据结构之二叉查找树
- cisco 反转多路复用的妙用
- Microsoft .NET Framework 4 安装 1603错误的解决
- 学习网络技术的一些常用论坛和网站
- Photoshop2019 安装破解
- 兄弟Brother MFC-T800W 驱动
- java 汉字转拼音原理_java 汉字转拼音
- SPI 及 NOR Flash 介绍
- 航嘉电源的通病维修方法!!
- Matlab实验(二)
热门文章
- 下载调试接口 JTAG vs SWD
- FlinkSQL IncompatibleClassChangeError: Implementing class
- 孩子为什么要上托育园?这8项技能,在家学不到!
- x11vnc安装及使用
- 商业社会成功必备16条商规
- WinRAR制作有密码的自解压文件
- python+selenium+Chrome(无头版)爬取国家地表水水质自动监测实时数据发布系统(修改版)——动态网页爬虫
- C/C++编程 获取调用其他程序的输出、匿名管道读写
- html表单提交前检查,form的onsubmit事件--表单提交前的验证最佳实现方式
- 故障诊断专家系统研究之三-----知识表示结构