Skip to content

Instantly share code, notes, and snippets.

@luabida
Created January 15, 2023 13:31
Show Gist options
  • Save luabida/091ca8f51862f134af25451f9c0c9ba4 to your computer and use it in GitHub Desktop.
Save luabida/091ca8f51862f134af25451f9c0c9ba4 to your computer and use it in GitHub Desktop.
Calculates the first and the last day of previous datetime month.
from datetime import datetime
import calendar
def calc_last_month_range(date: datetime) -> tuple:
if date.month == 1:
ini_date = datetime(date.year - 1, 12, 1)
else:
ini_date = datetime(date.year, date.month - 1, 1)
end_date = datetime(
ini_date.year,
ini_date.month,
calendar.monthrange(ini_date.year, ini_date.month)[1],
)
return ini_date, end_date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment