ayyappan a ayyappan a - 3 months ago 16
ASP.NET (C#) Question

How do I find the time difference between two datetime objects?

How do I find the time difference between two datetime objects in ASP.NET?

Answer

The following example demonstrates how to do this:

DateTime a = new DateTime(2010, 05, 12, 13, 15, 00);
DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
Console.WriteLine(b.Subtract(a).TotalMinutes);

When executed this prints "30" since there is a 30 minute difference between the date/times.

The result of DateTime.Subtract(DateTime x) is a TimeSpan Object which gives other useful properties.