获取上月开始结束日期

方法一

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获取上月、当月、下月的开始和结束日期的更多相关文章

  1. ASP获取上月本月下月的第一天和最后一天

    上月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...

  2. JS获取本周、本季度、本月、上月、本年的开始日期、结束日期

    /** * 获取本周.本季度.本月.上月的开始日期.结束日期 */ var now = new Date(); //当前日期  var nowDayOfWeek = now.getDay(); //今 ...

  3. 使用shell/python获取hostname/fqdn释疑

    一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...

  4. python 获取日期

    转载   原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...

  5. python获取字母在字母表对应位置的几种方法及性能对比较

    python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...

  6. python获取文件大小

    python获取文件大小 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import os # 字节bytes转化kb\m\g def formatSiz ...

  7. python 获取一个列表有多少连续列表

    python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:

  8. [python实用代码片段]python获取当前时间的前一天,前一周,前一个月

    python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...

  9. Python获取目录、文件的注意事项

    Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...

  10. Python 获取 网卡 MAC 地址

    /*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: ...

随机推荐

  1. Ubuntu 中科大源的使用

    官方网址: https://mirrors.ustc.edu.cn/help/ubuntu.html

  2. 让 Win8.1 微软拼音新体验模式支持 Metro 应用

    内容回顾: [1]十个步骤找回 Win8 中的微软拼音新体验模式 [2]发布个工具,一键恢复Win8/8.1中的微软拼音长句模式(新体验模式) 在 [1] 中,我们找回了 Win8 里的微软拼音新体验 ...

  3. 数位排序【第十三届蓝桥杯省赛C++C组】

    数位排序 小蓝对一个数的数位之和很感兴趣,今天他要按照数位之和给数排序. 当两个数各个数位之和不同时,将数位和较小的排在前面,当数位之和相等时,将数值小的排在前面. 例如,\(2022\) 排在 \( ...

  4. [LeetCode]最大连续1的个数

    题目 代码 class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { int length= ...

  5. python实现简单信息收集

    python实现简单信息收集 import whois import socket import sys def Query(domain): ip = socket.gethostbyname(st ...

  6. 文本纠错:提升OCR任务准确率的方法理解

    ​文本纠错:提升OCR任务准确率的方法理解 摘要:错字率是OCR任务中的重要指标,文本纠错需要机器具备人类水平相当的语言理解能力.随着人工智能应用的成熟,越来越多的纠错方法被提出. 近年来深度学习在O ...

  7. Node.js学习笔记----day04

    认真学习,认真记录,每天都要有进步呀!!! 加油叭!!! 一.Express 原生的http在某些方面上不足以满足我们的开发需求,所以我们需要使用框架来提高我们的开发效率,框架的目的就是提高开发效率, ...

  8. 基于APIView&ModelSerializer写接口

    目录 基于APIView&ModelSerializer写接口 一.首先准备前提工作 1.模型代码 2.路由代码 3.视图代码 二.继承Serializer序列化定制字段的三种方法 1.通过s ...

  9. 通过pdf模板,填充内容,生成pdf文件---JAVA

    1 概述 我们通常会遇到需要生成某些固定格式,但是内容不同的文件,那么我们就可以使用⽤Adobe Acrobat DC来创建pdf模块,然后通过代码对模板进行填充,生成pdf文件 2 创建一个pdf模 ...

  10. Fiddler V5.0 英文/汉化 Windows 抓包工具 【12月29日亲测有效】

    前言 Fiddlerr 功能强大的抓包工具,Web调试工具,HTTP协议抓包调试工具.它能够捕获浏览器和程序的所有http/https通信连接,可以针对访问请求,分析请求数据报文.设置断点.调试web ...