Difference between two DateTimes C#?

abmv picture abmv · May 10, 2009 · Viewed 121.9k times · Source

I need a function that can return the difference between the below two dates as 24.

DateTime a = new DateTime(2008, 01, 02, 06, 30, 00);
DateTime b = new DateTime(2008, 01, 03, 06, 30, 00);

Answer

Joey Robert picture Joey Robert · May 10, 2009

You can do the following:

TimeSpan duration = b - a;

There's plenty of built in methods in the timespan class to do what you need, i.e.

duration.TotalSeconds
duration.TotalMinutes

More info can be found here.