We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,277 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to get difference between given two times

hi,
i have three text boxes tb1 has timeIn tb2 has TimeOut and i have a Button which shows the time duration between them i have used time diffrence method bt it is unable to calculate the duration it only gives the difference which create problem when time is greater than 24 hrs

5
Contributors
6
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
7
Views
de Source
Light Poster
49 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

You can use a TimeSpan

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

Above poster is correct use a timespan. Here is an example.

            Timespan ts = DateTime1 - DateTime2;
jbrock31
Junior Poster in Training
79 posts since Oct 2008
Reputation Points: 22
Solved Threads: 16
Skill Endorsements: 0

or:

TimeSpan ts = DateTime1.Subtract(DateTime2); //date1 is newer then date2
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13

thanx for the responce but timespan is nt enough intelligent and does not work properly because when i enter time1 as 23:00 and time2 as 8:00 (in 24 hrs) it gives -15:00 but the answer should be 9:00
whereas my logic is to create a counter which should b enough intelligent that it will start the counter from the value in tb1 and stops till the value in tb2 but i don't know much about counter how to handle it i want something like this
tb1 = 23:00:00 1-jan-12
tb2 = 05:00:00 3-jan-12
answer should be
tb3 = 30:00:00

de Source
Light Poster
49 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
  1. if you would provide date too, it would calculate it correctly.
  2. or try somethig like:

     private void button2_Click(object sender, EventArgs e)
     {
         DateTime t1 = Convert.ToDateTime(textBox1.Text);
         DateTime t2 = Convert.ToDateTime(textBox2.Text);
         if (t2 < t1)
            t2 = t2.AddDays(1);
         TimeSpan ts = t2.Subtract(t1);
         MessageBox.Show(string.Format("The difference is {0}:{1}", ts.Hours, ts.Minutes));
     }
    

Dont forget to add some "protection" to dates, in case if there is an error input, like:

     DateTime t1;
     if(DateTime.TryParse(textBox1.Text, our t1))
     {
         //go on... all ok
     }
     else
         MessageBox.Show("Wrong input time or date...");

Hope it helps,
bye

Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13

If you ask me, you better use a standard control DateTimePicker for determination of time. I hope you find this feature can help.

    public static int TimeDiff(string From, string To)
    {
        int result = 0;
        DateTime Start = new DateTime();
        DateTime End = new DateTime();

        Start = DateTime.Parse(From);
        End = DateTime.Parse(To);
        result = End.CompareTo(Start);

        return result;
    }
darko444
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0746 seconds using 2.65MB