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 地址 * 说明: ...
随机推荐
- (一)elasticsearch 编译和启动
1.准备 先从github官网上clone elasticsearch源码到本地,选择合适的分支.笔者这里选用的是7.4.0(与笔者工作环境使用的分支一致),此版本编译需要jdk11. 2.编译 Re ...
- NOIP2018 解题报告
NOIP2018 解题报告 前记 在本届noip,作为第一年参加提高组的我,感受到了各位大佬神仙恐怖如斯的实力.身在弱省,但是依旧难以取得成绩,果然oi赛场,菜是原罪 好了,到了赛后,还是总结一下题目 ...
- absolute定位后居中的方法
要求 : 子级需要加动画上下动起来,并且需要在全屏居中 html代码如下: <div class="row m-0 w-100 mybanner"> <!-- 左 ...
- 关于 Dev-C++ 中缺少 iconv.h 的问题
前言 在 C++ 中有个扩展库 ext,里面有一些黑科技(hash, splay, binomial_heap 等等), 在 Windows 环境中,我们运行 Dev-C++ 并在头文件写 #incl ...
- Metasploit2通关教程
Metasploitable2靶机介绍: Metasploitable2 虚拟系统是一个特别制作的ubuntu操作系统,本身设计作为安全工具测试和演示常见漏洞攻击.这个版本的虚拟系统兼容VMware. ...
- 函数式编程思想概述-冗余的Runnable代码
函数式编程思想概述 在数学中,函数就是有输入量.输出星的一套计算方案,也就是"拿什么东西做什么事情".相对而言,面向对象过分强调"必须通过对象的形式来做事情", ...
- JSP第五次作业
1.教材P78-79 例4-9 1 <%@ page language="java" import="java.util.*" pageEncoding ...
- ChatGPT 背后核心技术的白话版
本文是关于ChatGPT 背后核心技术实现的一个通俗白话版,不涉及到的AI具体实现的技术细节哦. 在编排上增加了一些分割,内容具体如下: LLMs(大型语言模型) 如果将ChatGPT比作是动物,它就 ...
- 【踩坑记录】单测中@PostConstruct多次执行
问题复现: 单测中@PostConstruct修饰的方法被多次执行 原因: @PostConstruct在Spring中常用于在构造函数后初始化对象,执行顺序如下: 构造方法->成员变量注入-& ...
- 分布式id的方案
1 简介 在分布式系统架构中,通常会涉及到分布式全局唯一ID的生成 在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识. 如在金融.电商.支付.等产品的系统中,数据日渐增长,对数据分库分表后需 ...