python获取上月、当月、下月的开始和结束日期
获取上月开始结束日期
方法一
import datetime
def get_date_of_last_month(form="%Y-%m-%d"):
"""
获取上月开始结束日期
:param form 返回值显示格式
:return: str,date tuple
"""
today = datetime.date.today()
end_of_last_month = today - datetime.timedelta(today.day)
begin_of_last_month = datetime.date(end_of_last_month.year, end_of_last_month.month, 1)
return begin_of_last_month.strftime(form), end_of_last_month.strftime(form)
方法二
import datetime
import calendar
def get_date_of_last_month(form="%Y-%m-%d"):
"""
获取上月开始结束日期
:param form 返回值显示格式
:return: str,date tuple
"""
today = datetime.date.today()
year = today.year
month = today.month
if month == 1:
year -= 1
month = 12
else:
month -= 1
begin_of_last_month = datetime.date(year, month, 1).strftime(form)
_, day = calendar.monthrange(year, month)
end_of_last_month = datetime.date(year, month, day).strftime(form)
return begin_of_last_month, end_of_last_month
方法三
import datetime
def get_date_of_last_month(form="%Y-%m-%d"):
"""
获取上月开始结束日期
:param form 返回值显示格式
:return: str,date tuple
"""
today = datetime.date.today()
year = today.year
month = today.month
if month == 1:
begin_of_last_month = datetime.date(year - 1, 12, 1).strftime(form)
else:
begin_of_last_month = datetime.date(year, month - 1, 1).strftime(form)
end_of_last_month = (datetime.date(year, month, 1) + datetime.timedelta(-1)).strftime(form)
return begin_of_last_month, end_of_last_month
获取当月开始结束日期
import datetime
import calendar
def get_date_of_month(form="%Y-%m-%d"):
"""
获取当月开始结束日期
:param form 返回值显示格式
:return: str,date tuple
"""
today = datetime.date.today()
year = today.year
month = today.month
begin_of_month = datetime.date(year, month, 1).strftime(form)
_, day = calendar.monthrange(year, month)
end_of_month = datetime.date(year, month, day).strftime(form)
return begin_of_month, end_of_month
获取下月开始结束日期
方法一
import datetime
import calendar
def get_date_of_next_month(form="%Y-%m-%d"):
"""
获取下月开始结束日期
:param form 返回值显示格式
:return: str,date tuple
"""
today = datetime.date.today()
_, day = calendar.monthrange(today.year, today.month)
begin_of_next_month = today + datetime.timedelta(day - today.day + 1)
_, day = calendar.monthrange(begin_of_next_month.year, begin_of_next_month.month)
end_of_next_month = datetime.date(begin_of_next_month.year, begin_of_next_month.month, day)
return begin_of_next_month.strftime(form), end_of_next_month.strftime(form)
方法二
import datetime
import calendar
def get_date_of_next_month(form="%Y-%m-%d"):
"""
获取下月开始结束日期
:param form 返回值显示格式
:return: str,date tuple
"""
today = datetime.date.today()
year = today.year
month = today.month
if month == 12:
year += 1
month = 1
else:
month += 1
begin_of_next_month = datetime.date(year, month, 1).strftime(form)
_, day = calendar.monthrange(year, month)
end_of_next_month = datetime.date(year, month, day).strftime(form)
return begin_of_next_month, end_of_next_month
python获取上月、当月、下月的开始和结束日期的更多相关文章
- ASP获取上月本月下月的第一天和最后一天
上月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...
- JS获取本周、本季度、本月、上月、本年的开始日期、结束日期
/** * 获取本周.本季度.本月.上月的开始日期.结束日期 */ var now = new Date(); //当前日期 var nowDayOfWeek = now.getDay(); //今 ...
- 使用shell/python获取hostname/fqdn释疑
一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...
- python 获取日期
转载 原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...
- python获取字母在字母表对应位置的几种方法及性能对比较
python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...
- python获取文件大小
python获取文件大小 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import os # 字节bytes转化kb\m\g def formatSiz ...
- python 获取一个列表有多少连续列表
python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:
- [python实用代码片段]python获取当前时间的前一天,前一周,前一个月
python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...
- Python获取目录、文件的注意事项
Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...
- Python 获取 网卡 MAC 地址
/*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: ...
随机推荐
- emqtt-bench
安装 安装环境:Centos7 安装包:emqtt-bench-0.4.6-alpha.2-centos7-amd64.tar.gz 建议使用已编译好的发行包进行安装,源码编译已踩坑. # 创建存放目 ...
- SEO关键词布局方法
关键词在<title>标签. description属性. keywords属性中是如何布局的. 1.<title>标签中布局关键词 <title>标签是用来定义网 ...
- java中锁的概念/介绍
前言 Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高的效率.本文旨在对锁相关源码(本文中的源码来自JDK 8和Netty 3.10.6).使用场景进行举例,为读者介绍 ...
- 100 行 shell 写个 Docker
作者:vivo 互联网运维团队- Hou Dengfeng 本文主要介绍使用shell实现一个简易的Docker. 一.目的 在初接触Docker的时候,我们必须要了解的几个概念就是Cgroup.Na ...
- 重新思考 Vue 组件的定义
重新总结组件的定义 这是官方对组件的定义:组件允许我们将 UI 划分为独立的.可重用的部分,并且可以对每个部分进行单独的思考.在实际应用中,组件常常被组织成层层嵌套的树状结构. 对于 Vue 开发经验 ...
- Nginx 安装perl
1 安装包下载 https://www.cpan.org/src获取最新偶数版本下载链接并替换(偶数版本为稳定版) 2 上传到服务器解压 tar -zxvf perl-5.36.0.tar.gz 3 ...
- Docker安装Tomcat应用服务器
1.安装镜像 1. Install the image: 可以先到https://hub.docker.com/ 搜索镜像 You can get there first. https://hub. ...
- 【TS】函数重载--可选参数--默认参数
可选参数--默认参数 在ts中定义的数据类型,某些情况下只需要传入定义数据类型的一部分参数,比如:id .name.age.address,此时需要修改用户的名称,那么只需要传入id.name就够了: ...
- 部署Kubernetes v1.22.10高可用集群
一.概述 Kubernetes集群控制平面(Master)节点右数据库服务(Etcd)+其它服务组件(Apiserver.Controller-manager.Scheduler等)组成:整个集群系统 ...
- 【KAWAKO】MNN-1.2.0版本交叉编译遇到的错误与解决方法
目录 在使用gcc-linaro-7.5.0-aarch64-linux-gnu.gcc-linaro-6.3.1-aarch64-linux-gnu交叉编译链对MNN1.2.0进行交叉编译的过程中, ...