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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With