取一个月的第一天和最后一天

from datetime import datetime, timedelta
def get_month_b_e_day(month):
    month_begin_day = month + "01"
    next_month = int(month) + 1
    if(next_month % 100 == 13):
        next_month = next_month - 12 + 100
    month_end_day = (datetime(int(str(next_month)[0:4]), int(str(next_month)[4:6]), 1) - timedelta(days=1)).strftime("%Y%m%d")
    begin_time = "-".join([month_begin_day[:4], month_begin_day[4:6], month_begin_day[6:8]]) + " " + "00:00:00"
    end_time = "-".join([month_end_day[:4], month_end_day[4:6], month_end_day[6:8]]) + " " + "23:59:59"
    return begin_time,end_time

begin_time,end_time = get_month_b_e_day("201912")
print(begin_time)
print(end_time)
posted @ 2020-01-13 15:18  骑驴老神仙  阅读(256)  评论(0编辑  收藏  举报