Answers for "python get last date of month"

2

python datetime last day of month

>>import calendar
>>year, month = 2016, 12
>>calendar.monthrange(year, month)[1]
31
Posted by: Guest on February-08-2021
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
0

get last day of month python

import datetime
def last_day_of_month(any_day):
	# get close to the end of the month for any day, and add 4 days 'over'
	next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
	# subtract the number of remaining 'overage' days to get last day of current month, or said programattically said, the previous day of the first of next month
	return next_month - datetime.timedelta(days=next_month.day)


last_date_of_month(datetime.date.today())
Posted by: Guest on November-06-2021

Code answers related to "python get last date of month"

Python Answers by Framework

Browse Popular Code Answers by Language