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
posted @ 2022-05-17 16:54  cnblogs用户  阅读(3693)  评论(0编辑  收藏  举报