How to check if a date has passed in C#?
public bool HasPassed2hoursFrom(DateTime fromDate, DateTime expireDate)
{
return expireDate - fromDate > TimeSpan.FromHours(2);
}
public bool HasExpired(DateTime now)
{
string expires = ReadDateFromDataBase(); // output example: 21/10/2011 21:31:00
DateTime Expires = DateTime.Parse(expires);
return now.CompareTo(Expires.Add(new TimeSpan(2, 0, 0))) > 0;
}
But since DateTime.Now is very fast and you don't need to pass it as function parameter...
public bool HasExpired()
{
string expires = ReadDateFromDataBase(); // output example: 21/10/2011 21:31:00
DateTime Expires = DateTime.Parse(expires);
return DateTime.Now.CompareTo(Expires.Add(new TimeSpan(2, 0, 0))) > 0;
}
bool HasPassed2hoursFrom(DateTime now, DateTime expires)
{
return (now - expires).TotalHours >= 2;
}