Reputation: 287
I am programming small page and I want to display expiration time of authentication ticket. I mean not the end, but remaining time. Current code is follows:
DateTime cas = (DateTime)ticket.Expiration.Date;
DateTime cas1 = DateTime.Now;
DateTime cas2 = cas1.Subtract(cas);
However, VS says "Cannot implicitly convert system.timespan into system datetime". Pls help. Thanks a lot.
Upvotes: 0
Views: 830
Reputation: 18654
You can use operator overloading. The type returned by subtracting one DateTime from another is a TimeSpan:
TimeSpan remainingTime = ticket.Expiration - DateTime.Now;
Upvotes: 0
Reputation: 37660
Substract method returns a timespan, not a Datetime. Try this :
Timespan cas2 = cas.Subtract(cas1);
[edit] following the comments, the code that should works is simply :
TimeSpan remaining = ticket.Expiration.Substract(DateTime.Now);
You don't have to case Expiration, as it's a DateTime property.
Upvotes: 1