Solved

How do I subtract time  in c#.net?

Posted on 2008-10-15
4
1,634 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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