Answers for "python get first and last day of month"

1

python first day of last month

from datetime import date, timedelta

last_day_of_prev_month = date.today().replace(day=1) - timedelta(days=1)

start_day_of_prev_month = date.today().replace(day=1) - timedelta(days=last_day_of_prev_month.day)

# For printing results
print("First day of prev month:", start_day_of_prev_month)
print("Last day of prev month:", last_day_of_prev_month)
Posted by: Guest on September-01-2020
1

first day of the month python

from datetime import datetime

datetime.today().replace(day=1)
Posted by: Guest on December-08-2020
2

python find end of month

import calendar
from datetime import datetime

year = 2020
month = 1

rng = calendar.monthrange(year, month)  # (2, 31)
last_day = datetime(year, month, rng[1])

# calendar.monthrange returns tuple in the format of
# (<day of week>, <last day>). day of week is 0 indexed
# starting on Monday
#
# 0 = Monday
# 1 = Tuesday
# 2 = Wednesday
# 3 = Thursday
# 4 = Friday
# 5 = Saturday
# 6 = Sunday
Posted by: Guest on October-01-2020

Code answers related to "python get first and last day of month"

Python Answers by Framework

Browse Popular Code Answers by Language