Created
January 15, 2023 13:31
-
-
Save luabida/091ca8f51862f134af25451f9c0c9ba4 to your computer and use it in GitHub Desktop.
Calculates the first and the last day of previous datetime month.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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