python当前日期月初和月末_python时间计算:当天、前一天、月初、月末、季初、季末、半年初、半年末、年初、年末...

月初:

newdate = date.replace(day=1)

condtions = {'datadate': newdate}

月末:

year = date.year

month = date.month

a, b = calendar.monthrange(year, month)           # a,b——weekday的第一天是星期几(0-6对应星期一到星期天)和这个月的所有天数

newdate = datetime.datetime(year=year, month=month, day=b) # 构造本月月末datetime

condtions = {'datadate': newdate}

上月末:

date_now = date.replace(day=1)

newdate = date_now + datetime.timedelta(days=-1)

condtions = {'datadate': newdate}

上月初:

date_now = date.replace(day=1)

date_now = date_now + datetime.timedelta(days=-1)

newdate = datetime.datetime(date_now.year, date_now.month, 1)

condtions = {'datadate': newdate}

年初:

newdate =

你可能感兴趣的:(python当前日期月初和月末)