获取当前月、上一个月、下一个月、月份第一天、月份最后一天
import calendarfrom datetime import datetimefrom dateutil.relativedelta import relativedeltaclass DateTimeUtil():def get_cur_month(self):# 获取当前月return datetime.now().strftime("%Y-%m")def get_last_month(self, number=1):# 获取前几个月month_date = datetime.now().date() - relativedelta(months=number)return month_date.strftime("%Y-%m")def get_next_month(self, number=1):# 获取后几个月month_date = datetime.now().date() + relativedelta(months=number)return month_date.strftime("%Y-%m")def get_cur_month_start(self):# 获取当前月的第一天month_str = datetime.now().strftime('%Y-%m')return '{}-01'.format(month_str)def get_cur_month_end(self):# 获取当前月的最后一天'''param: month_str 月份,-04'''# return: 格式 %Y-%m-%dmonth_str = datetime.now().strftime('%Y-%m')year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])end = calendar.monthrange(year, month)[1]return '{}-{}-{}'.format(year, month, end)def get_last_month_start(self, month_str=None):# 获取上一个月的第一天'''param: month_str 月份,-04'''# return: 格式 %Y-%m-%dif not month_str:month_str = datetime.now().strftime('%Y-%m')year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])if month == 1:year -= 1month = 12else:month -= 1return '{}-{}-01'.format(year, month)def get_next_month_start(self, month_str=None):# 获取下一个月的第一天'''param: month_str 月份,-04'''# return: 格式 %Y-%m-%dif not month_str:month_str = datetime.now().strftime('%Y-%m')year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])if month == 12:year += 1month = 1else:month += 1return '{}-{}-01'.format(year, month)def get_last_month_end(self, month_str=None):# 获取上一个月的最后一天'''param: month_str 月份,-04'''# return: 格式 %Y-%m-%dif not month_str:month_str = datetime.now().strftime('%Y-%m')year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])if month == 1:year -= 1month = 12else:month -= 1end = calendar.monthrange(year, month)[1]return '{}-{}-{}'.format(year, month, end)def get_next_month_end(self, month_str=None):# 获取下一个月的最后一天'''param: month_str 月份,-04'''# return: 格式 %Y-%m-%dif not month_str:month_str = datetime.now().strftime('%Y-%m')year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])if month == 12:year += 1month = 1else:month += 1end = calendar.monthrange(year, month)[1]return '{}-{}-{}'.format(year, month, end)if __name__ == '__main__':# 获取当前月print('当前月', DateTimeUtil().get_cur_month())# 获取上一个月print('上一个月', DateTimeUtil().get_last_month())# 获取上两个月print('上两个月', DateTimeUtil().get_last_month(number=2))# 获取下一个月print('下一个月', DateTimeUtil().get_next_month())# 获取下两个月print('下两个月', DateTimeUtil().get_next_month(number=2))# 获取当前月的第一天print('当前月的第一天', DateTimeUtil().get_cur_month_start())# 获取当前月的最后一天print('当前月的最后一天', DateTimeUtil().get_cur_month_end())# 获取上个月的第一天print('上个月的第一天', DateTimeUtil().get_last_month_start())# 获取下个月的第一天print('下个月的第一天', DateTimeUtil().get_next_month_start())# 获取上个月的最后一天print('上个月的最后一天', DateTimeUtil().get_last_month_end())# 获取下个月的最后一天print('下个月的最后一天', DateTimeUtil().get_next_month_end())
输出:
当前月 -04
上一个月 -03
上两个月 -02
下一个月 -05
下两个月 -06
当前月的第一天 -04-01
当前月的最后一天 -4-30
上个月的第一天 -3-01
下个月的第一天 -5-01
上个月的最后一天 -3-31
下个月的最后一天 -5-31