문제

I'm working on a Calendar app in Python that is checking several events within different parameters. One of the issues I'm having is that in several points of my code I use an example shown below to verify that a particular event is "today".

if (eventDateTime - TheCurrentDateTime).days = 0: print "Your event is today."

The issue there is that if it's say, Monday and I have an event happening on Tuesday at 8:00A.M. - once it turns to 8:01A.M. Monday morning, the timedelta subtraction check for .days becomes 0, because their are only hours left before the event, not days. So, being that my Calendar is checking every minute for new updates and such, after that time has past, for the rest of Monday, it continues to report that my Tuesday event is "today."

I'd like to create a multi-condition if statement that somehow keeps it from saying "Your event is today." unless it's past midnight and actually into the next day (day of the event).

Thanks

도움이 되었습니까?

해결책

Just compare date, without time:

TheCurrentDate = TheCurrentDateTime.date()
if eventDateTime.date() == TheCurrentDate:
    print "Your event is today."

다른 팁

You can use (eventDateTime - TheCurrentDateTime).total_seconds() < 86400 to check for "less than 24 hours".

However, for "is today" I'd expect it to compare the actual day, not the number of hours. So compare eventDateTime.date() == TheCurrentDateTime.date()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top