How do I subtract time  in

Posted on 2008-10-15
Last Modified: 2008-10-16
How do I subtract time  in

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
  • 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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 53
What .NET URL re-routing tool did I use? 2 54
Help with C#, MVC, razor. 6 34
Finally almost got it 5 36
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now