获取上月开始结束日期

方法一

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. 初始化一个GCP项目并用gcloud访问操作

    1 简介 谷歌云GCP(Google Cloud Platform)是由Google提供的云平台,还是为用户提供了许多免费的产品,还是可以尝试一下的.对于学习或者小项目,都可以使用. 2 创建一个新项 ...

  2. 安装postcss-px-to-viewport 配置postcss.config.js 报错Error: true is not a PostCSS plugin

    因项目需要,用户突然要坚持小屏幕也要观看大屏代码,临时解决方案是加了一个postcss-px-to-viewport ,安装过程中报错Error: true is not a PostCSS plug ...

  3. odoo中的字段创建后,不可以编辑

  4. P6327 区间加区间sin和 题解

    P6327 区间加区间sin和 题解 题目描述 给出一个长度为 \(n\) 的整数序列 \(a_1,a_2,\ldots,a_n\),进行 \(m\) 次操作,操作分为两类. 操作 \(1\):给出 ...

  5. sync.Once 使用及解析

    目录 前言 1. sync.Once 简介 2. sync.Once 源码解析 2.1 为什么 done 作为第一个字段 2.2 Do 方法的实现细节 2.3 其他重要细节 3. sync.Once ...

  6. Loadrunner的函数收集

    1.int web_url( const char *StepName, const char *url, <List of Attributes>, [EXTRARES, <Lis ...

  7. 新版Bing 搜索后台的.NET 技术栈

    微软在今天在Redmond 线下举办媒体一场活动,发布了新版的微软必应,在桌面上推出了测试版,移动版也即将推出.微软首席执行官纳德拉称Al-powered搜索为公司自云15年以来最大的事情. 2023 ...

  8. 11月17日内容总结——黏包现象、struct模块和解决黏包问题的流程、UDP协议、并发编程理论、多道程序设计技术及进程理论

    目录 一.黏包现象 什么是黏包 黏包现象产生的原因 二.struct模块及解决黏包问题的流程 struct模块 解决黏包问题初级版本 解决过程中遇到的问题 解决黏包问题终极解决方案 三.粘包代码实战 ...

  9. Thymeleaf中判断Security权限 - SpringBoot

    参考:https://blog.csdn.net/perfect_red/article/details/110821582

  10. 使用Addressables.LoadAssetAsync<Asset>(target)加载unity资源,不止是gameobject

    要声明的方法: public static async Task<string> ReadJsonData(string target){  TextAsset jsonDataObjec ...