'How to compare two dates based on Month and Year only in python

I need to compare two dates based on month and year only, without considering the day or time. I tried following but it does not give correct result:

if lastMonthAllowed.month > lastUserUploadMonth.month and lastMonthAllowed.year > lastUserUploadMonth.year:
    #do something

Is there any simple way to achieve this?



Solution 1:[1]

You could do it like this:

import datetime

def trunc_datetime(someDate):
    return someDate.replace(day=1, hour=0, minute=0, second=0, microsecond=0)

a = trunc_datetime(datetime.datetime(2018,1,15))
b = trunc_datetime(datetime.datetime(2018,1,20))

print(a == b)
>>>True

Solution 2:[2]

You could use datetime.datetime, which has a year and a month attribute. Check this link.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 wohe1