Solved

timespan difference between 2 times

Posted on 2007-11-27
4
1,746 Views
Last Modified: 2013-12-16
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
Comment
Question by:MSAIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Accepted Solution

by:
sjturner2 earned 250 total points
ID: 20359299
The attached snippet should help you, the output is:

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));

Open in new window

0
 
LVL 29

Assisted Solution

by:Göran Andersson
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"

Open in new window

0
 
LVL 1

Expert Comment

by:Computer101
ID: 20615317
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question