I have a day/month
string, I want to convert that string to date object and compare the last day of that month to another date
Example:
For 08/2021
(august, 2021) I want to compare the last day of that month (31-08-2021) to another date (date field),
For 02/2020
I what to compare 29-02-2020 < another_date (date field)
For 02/2021
I what to compare 28-02-2020 < another_date (date field)
You can use calendar.monthrange
to find the last day in the month if you don't want to add dateutil.
import calendar
from datetime import datetime
def get_last_day_date(year_month_str):
date = datetime.strptime(year_month_str, "%m/%Y")
last_day = calendar.monthrange(date.year, date.month)[1]
return datetime(date.year, date.month, last_day)
get_last_day_date("08/2020")
# datetime.datetime(2020, 8, 31, 0, 0)