[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I subtract time  in c#.net?

Posted on 2008-10-15
4
Medium Priority
?
1,655 Views
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
0
Comment
Question by:wademi
  • 4
4 Comments
 
LVL 6

Expert Comment

by:RishadanPort
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;
0
 
LVL 6

Expert Comment

by:RishadanPort
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

0
 
LVL 6

Expert Comment

by:RishadanPort
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");
0
 
LVL 6

Accepted Solution

by:
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

873 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