python get last day of month code example

Example 1: python get day name

import datetime
now = datetime.datetime.now()
print(now.strftime("%A"))

Example 2: 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)

Example 3: python datetime last day of month

>>import calendar
>>year, month = 2016, 12
>>calendar.monthrange(year, month)[1]
31

Example 4: first day of the month python

from datetime import datetime

datetime.today().replace(day=1)