我只需要根据月份和年份比较两个日期,而不考虑日期或时间。我尝试了以下操作,但没有给出正确的结果:
if lastMonthAllowed.month > lastUserUploadMonth.month and lastMonthAllowed.year > lastUserUploadMonth.year:
#do something
Run Code Online (Sandbox Code Playgroud)
有什么简单的方法可以实现这一目标吗?
你可以这样做:
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
Run Code Online (Sandbox Code Playgroud)