polohy
polohy

Reputation: 287

Display ticket remaining time

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

Answers (2)

RickNZ
RickNZ

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

Steve B
Steve B

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

Related Questions