def get_month_period(period_range):
"""
return a start date and a end date of x complete previous month
:param period_range: number of months
:return: date_from (datetime), date_to (datetime)
"""
today = datetime.datetime.today().replace(hour=23, minute=59, second=59, microsecond=999999)
current_month = today.replace(day=1)
last_day_of_current_month = calendar.monthrange(today.year, today.month)[1]
if today.day == last_day_of_current_month:
date_from = current_month + relativedelta(months=-period_range - 1)
date_to = today
else:
date_from = current_month + relativedelta(months=-period_range)
date_to = current_month - datetime.timedelta(days=1)
return date_from, date_to
评论列表
文章目录