[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


How do I subtract time  in c#.net?

Posted on 2008-10-15
Medium Priority
Last Modified: 2008-10-16
How do I subtract time  in c#.net?

How do I subtract for example (7:40 pm  - 7:20 pm) to result in just minutes.

My times are represented in this exact format
Question by:wademi
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

Expert Comment

ID: 22726851
Convert both times into second format...

Then subtract seconds, and convert seconds into minutes.

Use this:

            TimeSpan time = <some time>;
            TimeSpan time2 = ....;
            int totalSeconds = time2.TotalSeconds - time.TotalSeconds;

Expert Comment

ID: 22726887
Further example listed below.

You may need to research a bit more on how to convert to a DateTime from a string
DateTime time = Convert.ToDateTime(<put time here>);
DateTime time2 = Convert.ToDateTime(<put time here>);
TimeSpan stime = new TimeSpan(time.Hour, time.Minute, time.Second);
TimeSpan stime2 = new TimeSpan(time2.Hour, time2.Minute, time2.Second);
int seconds = stime2.TotalSeconds - stime.TotalSeconds;
int minutes = seconds * 60;

Open in new window


Expert Comment

ID: 22726934
It turns out that this compiles, and runs correctly:

DateTime time = Convert.ToDateTime("7:40 pm");
DateTime time2 = Convert.ToDateTime("7:20 pm");

Accepted Solution

RishadanPort earned 2000 total points
ID: 22726986
Here is my full updated solution:
DateTime time = Convert.ToDateTime("7:40pm");
DateTime time2 = Convert.ToDateTime("7:20pm");
TimeSpan span = new TimeSpan(time.Hour, time.Minute, time.Second);
TimeSpan span2 = new TimeSpan(time2.Hour, time2.Minute, time2.Second);
MessageBox.Show("Total Minutes is " 
   + span.TotalMinutes - span2.TotalMinutes);

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 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