import calendar
 
 
def get_current_month_start_and_end(date):
    ""Given a date, the output date is 0 o 'clock on the first day of the month and 23 o 'clock on the last day of the month :return: the first day of the month and the last day of the month.""
    if date.count(The '-') != 2:
        raise ValueError('- is error')
    year, month = str(date).split(The '-') [0], str(date).split(The '-') [1]
    end = calendar.monthrange(int(year), int(month))[1]
    start_date = '%s-%s-01 00:00:00' % (year, month)
    end_date = '%s-%s-%s 23:59:59' % (year, month, end)
    return start_date, end_date
 
 
if __name__ == "__main__":
    pass
    print(get_current_month_start_and_end("2020-10-22"))
Copy the code