Back to previous

How to show DateTime as Time Left

I recently needed to show a datetime as amount of time left or past. This can be done by using TimeSpan, which you can get by subtracting one DateTime from another:

DateTime dt1 = new DateTime(2009, 7, 2);
DateTime dt2 = new DateTime(2009, 7, 3);
TimeSpan difference = dt2 - dt1;
// Then you can display things like difference.Days

See the docs on TimeSpan: http://msdn.microsoft.com/en-us/library/system.timespan.aspx

 

ShareThis

If you think this post is useful, please recommend me at the bottom of the page. ;)

Discussion