1,105,534 Community Members

how to get difference between given two times

Member Avatar
de Source
Junior Poster in Training
66 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

You can use a TimeSpan

Member Avatar
jbrock31
Junior Poster in Training
79 posts since Oct 2008
Reputation Points: 11 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

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

            Timespan ts = DateTime1 - DateTime2;
Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

or:

TimeSpan ts = DateTime1.Subtract(DateTime2); //date1 is newer then date2
Member Avatar
de Source
Junior Poster in Training
66 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
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

Member Avatar
darko444
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
    }
Question Answered as of 8 Months Ago by Mitja Bonca, jbrock31, thines01 and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: