Search code examples
delphidelphi-7

Calculate timeleft


I'm making an application that has a function that calculate time, if it's already past the expired time it should pop up a message saying "session has expired" something like that.

Here's I calculate time:

  CurrentTime := StrToDateTime('10/10/15 10:02');
  TimeExpired := StrToDateTime('10/10/15 10:00');

  iHours := HoursBetween(CurrentTime, TimeExpired );
  iMinutes := MinutesBetween(CurrentTime, TimeExpired )- (iHours * 60 );

Problem there is it doesn't give a negative value (-2).

I normally just calculate the hours and minutes left, but if my application crashes or accidentally closed, and the user reopened it again and it already past the expiration time, the minutes left increases instead of decreasing.

How do I know if the time already expired ?


Solution

  • You could simply check which time is greater. If CurrentTime > TimeExpired, then the expiration time is in the past, and the session has obviously expired.