Solved

# timespan difference between 2 times

Posted on 2007-11-27
1,733 Views
How do I get the differece in "mm:ss" of 2 timespans in C#
Time1 = "06:00 AM"
Time2 = "06:10 AM"
It must also automatically calc the correct span when it is
Time1 = "11:55 AM"
Time2 = "01:10 PM"

0
Question by:MSAIT

LVL 8

Accepted Solution

sjturner2 earned 250 total points
ID: 20359299

Hours: 0, Seconds 10
Hours: 1, Seconds 15
``````   DateTime d1 = new DateTime(2000, 12, 1, 6, 0, 0);

DateTime d2 = new DateTime(2000, 12, 1, 6, 10, 0, 0);

TimeSpan s = d2.Subtract(d1);

System.Diagnostics.Debug.WriteLine(

string.Format("Hours: {0}, Seconds {1}",

s.Hours,

s.Minutes));

DateTime d3 = new DateTime(2000, 12, 1, 11, 55, 0, 0);

DateTime d4 = new DateTime(2000, 12, 1, 13, 10, 0, 0);

TimeSpan s2 = d4.Subtract(d3);

System.Diagnostics.Debug.WriteLine(

string.Format("Hours: {0}, Seconds {1}",

s2.Hours,

s2.Minutes));
``````
0

LVL 29

Assisted Solution

Göran Andersson earned 250 total points
ID: 20359649
You can add and subtract TimeSpan values like any numeric value. You can add a TimeSpan value to a DateTime value. You can use DateTime.ParseExact to parse a string to a DateTime value. You can subtract two DateTime values to get a TimeSpan value:
``````TimeSpan Time1 = TimeSpan.FromHours(6);

TimeSpan Time2 = TimeSpan.FromHours(6) + TimeSpan.FromMinutes(10);

TimeSpan diff = time2 - Time1; // result is 10 minutes

Console.WriteLine((DateTime.Today + diff).ToString("hh:mm tt")); // outputs "00:10 AM"

Console.WriteLine((DateTime.Today + diff).ToString("HH:mm")); // outputs "00:10"

DateTime Date1 = DateTime.ParseExact("11:55 AM", "hh:mm tt", CultureInfo.InvariantCulture);

DateTime Date2 = DateTime.ParseExact("01:10 PM", "hh:mm tt", CultureInfo.InvariantCulture);

TimeSpan diff2 = Date2 - Date1; // result is 75 minutes

Console.WriteLine((DateTime.Today + diff2).ToString("hh:mm tt")); // outputs "01:15 AM"

Console.WriteLine((DateTime.Today + diff2).ToString("HH:mm")); // outputs "01:15"
``````
0

LVL 1

Expert Comment

ID: 20615317
Forced accept.

Computer101
0

## Featured Post

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…