Reputation: 2737
I need to calculate IsExpired in model if date is in past, but I need to check till 23: 59:59
So for example if expiry date is 2021-06-02 13:56:22 and today is 2021-06-03 13:56:22
Date will be expired
And if expiry date is 2021-06-03 13:56:22 and today is 2021-06-03 it will not be expired, because it's still not 2021-06-03
I wrote this code, but it's not working
public bool IsExpired => ExpirationDate > DateTime.Parse(DateTime.Now.ToShortDateString().Trim() + " 23:59:59");
How I can make it work?
Upvotes: 0
Views: 716
Reputation: 38767
If you just want to have IsExpired
be true if the day of expiration is before today, you probably just want this:
public bool IsExpired => ExpirationDate.Date < DateTime.Now.Date;
The .Date
part zeroes the time, so we do it to both to ensure that it's at the start of the day for both.
Taking your example of 2021-06-03 13:56:22 and 2021-06-03 00:00:00, we'd end up with the following comparison:
2021-06-03 < 2021-06-03 = false // not expired
P.S. If you want to include today in "expired", then you should change <
to <=
.
Upvotes: 2