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

You can use a TimeSpan

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

            Timespan ts = DateTime1 - DateTime2;

or:

TimeSpan ts = DateTime1.Subtract(DateTime2); //date1 is newer then date2

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

  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

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;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.