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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

12 Experts available now in Live!

Get 1:1 Help Now