ktm
ktm

Reputation: 6085

Subtracting date

How to do I subtract two dates and display days left?

expire (the expiry date) variable comes from the database.

expire = row.expire.ToString("dd. MMMM. yyyy");
dayleft = DateTime.Now.Subtract(expire).ToString();

Upvotes: 0

Views: 155

Answers (1)

Kristof Claes
Kristof Claes

Reputation: 10941

The Subtract method returns a TimeSpan, so you can do something like this:

DateTime expire = DateTime.Parse(row.expire);
TimeSpan difference = expire.Subtract(DateTime.Now);

double totalDaysLeft = difference.TotalDays; //eg. 3.69

int daysLeft = difference.Days; //eg. 3

Upvotes: 3

Related Questions