Python gets the first and last day of the previous month

import datetime


def get_last_month_start_end():
    """
    获取上月第一天和最后一天并返回(元组)
    example:
        now date:2020-03-06
        return:2020-02-01,2020-02-29
    :return:
    """
    today = datetime.date.today()
    last_day_of_last_month = datetime.date(today.year, today.month, 1) - datetime.timedelta(1)
    first_day_of_last_month = datetime.date(last_day_of_last_month.year, last_day_of_last_month.month, 1)
    return first_day_of_last_month, last_day_of_last_month


first_day_of_last_month, last_day_of_last_month = get_last_month_start_end()
print(first_day_of_last_month, last_day_of_last_month)

 

Guess you like

Origin blog.csdn.net/zhu6201976/article/details/104706911