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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值