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
本文来自博客园,仅供参考学习,如有不当之处还望不吝赐教,不胜感激!转载请注明原文链接:https://www.cnblogs.com/rong-z/p/16281492.html
作者:cnblogs用户
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix