python datetime get last day of month code example
Example 1: python find end of month
import calendar
from datetime import datetime
year = 2020
month = 1
rng = calendar.monthrange(year, month)
last_day = datetime(year, month, rng[1])
Example 2: python get day month year
import datetime
now = datetime.datetime.now()
print(now.year, now.month, now.day, now.hour, now.minute, now.second)
Example 3: 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)
print("First day of prev month:", start_day_of_prev_month)
print("Last day of prev month:", last_day_of_prev_month)