• 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
    
  • 相关阅读:
    TreeView拖动
    反射机制
    SQLServer2005/2008 XML数据类型操作
    开发与研发:一字之差的感想
    设置在64位机器上的IIS(IIS6/IIS7)兼容32位程序(64位ODBC和32位ODBC的问题同样适用)
    setTimeout和setInterval的使用
    Oracle 安装/使用、配置/卸载
    链接sql数据库以及Oracle 数据库和启动缓存以及停止缓存
    jQuery学习笔记—— .html(),.text()和.val()的使用
    C# List<T>用法
  • 原文地址:https://www.cnblogs.com/rong-z/p/16281492.html
Copyright © 2020-2023  润新知