获取上月开始结束日期

方法一

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. 【AI编译器原理】系列来啦!我们要从入门到放弃!

    随着深度学习的应用场景的不断泛化,深度学习计算任务也需要部署在不同的计算设备和硬件架构上:同时,实际部署或训练场景对性能往往也有着更为激进的要求,例如针对硬件特点定制计算代码. 这些需求在通用的AI框 ...

  2. 刷题笔记——1112:C语言考试练习题_一元二次方程

    题目 1112:C语言考试练习题_一元二次方程 代码 import math while True: try: a,b,c=map(float,input().strip().split()) del ...

  3. 一个关于sum over的疑问

    参考: http://www.cnblogs.com/lanzi/archive/2010/10/26/1861338.html select * from bitest.tmp_0222_zym ; ...

  4. flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.

    flutter 1.升级2.X在模型类中序列化JSON报错 Non-nullable instance field 'title' must be initialized. Try adding an ...

  5. vh 存在问题?试试动态视口单位之 dvh、svh、lvh

    大部分同学都知道,在 CSS 世界中,有 vw.vh.vmax.vmin 这几个与视口 Viewport 相关的单位. 正常而言: 1vw 等于1/100的视口宽度 (Viewport Width) ...

  6. RISC-V 平台移植 RTOS

        ARM 上移植实时操作系统大家可能比较熟悉,且例程较多,对于 RISC-V 内核的 MCU,可能相对比较陌生.下面结合 WCH 的 CH32V103 和 CH32V307 两款芯片来详细说下针 ...

  7. 【一句话】Java8后abstract class和interface的区别

    首先一句话: Java8后(1)interface支持default和static方法有实现,abstract class依然是抽象方法和非抽象方法,(2)可同时实现多个interface,(3)但成 ...

  8. 构建api gateway之 如何给openresty打patch

    如何给openresty打patch 由于很多功能实现的限制,我们不得不修改openresty,但我们又不一定能持续维护一个 openresty 分支,所有有了patch 这一操作. patch是怎么 ...

  9. C#IIS上传文件大小设置问题:maxRequestLength、maxAllowedContentLength

    system.web节点下的 maxRequestLength属性来指定最大上传文件的size,默认值是4M <system.web> <httpRuntime maxRequest ...

  10. 【RocketMQ】DLedger选主源码分析

    RocketMQ 4.5版本之前,可以采用主从架构进行集群部署,但是如果master节点挂掉,不能自动在集群中选举出新的Master节点,需要人工介入,在4.5版本之后提供了DLedger模式,使用R ...