none
How calculated the time difference between two hours HH:mm 12/24 hour time format RRS feed

  • Question

  • I want to calculate any time I enter text boxes. The encoding should calculate the hours entered in the text boxes.

    also I use the code like this

    private void simpleButton14_Click(object sender, EventArgs e)
            {
                textEdit7.Text = SaatFarki("05:30", "22:30");
                
                textEdit7.Text = SaatFarki(textEdit4.Text, textEdit6.Text);
                
            }
            private string SaatFarki(string basla, string bitir)
    
            {
                DateTime time1 = DateTime.ParseExact(basla, "HH:mm", CultureInfo.CurrentCulture);
                DateTime time2 = DateTime.ParseExact(bitir, "HH:mm", CultureInfo.CurrentCulture);
                TimeSpan time3 = time2 - time1;
                return $"{time3.Hours:00} : {time3.Minutes:00}";
    
            }
    I'm writing to the first text box 19:15 and I'm writing in the second textbox 02:30 

    it shows the result like this in third textbox -16:45

    but

    for example

    when I entered the textboxes this way, first texbox 02:30 second textbox 19:15

    shows the result like this in third textbox 16:45 

    why the above result shows with a minus sign? the below side that should be



    • Edited by BurakTurhan Tuesday, March 27, 2018 1:27 PM
    Tuesday, March 27, 2018 1:26 PM

All replies

  • Hello BurakTurhan,

    When dealing with time you need to involve some logic in it. When your result is less than 0 you need to add 24 hours to your result. Lets say you want to calculate from 10:00 to 14:00. if you do the math it's 4 hours right ? Let's say now you have 19:00 to 2:00 am. If you make the calculations you will have 7 hours. If you do the abstraction like normal the computer will return -17:00 right? well add 24 to it and you will get 7 which is what you want. So check if you have a negative time and then add 24 to it. Your code should look like this:

    private void simpleButton14_Click(object sender, EventArgs e)
            {
                textEdit7.Text = SaatFarki("05:30", "22:30");
                
                textEdit7.Text = SaatFarki(textEdit4.Text, textEdit6.Text);
                
            }
            private string SaatFarki(string basla, string bitir)
    
            {
                DateTime time1 = DateTime.ParseExact(basla, "HH:mm", CultureInfo.CurrentCulture);
                DateTime time2 = DateTime.ParseExact(bitir, "HH:mm", CultureInfo.CurrentCulture);
                TimeSpan time3 = time2 - time1;
                if (time3.TotalHours < 0)
                {
                     return $"{24 + time3.Hours:00} : {time3.Minutes:00}";
                }
                return $"{time3.Hours:00} : {time3.Minutes:00}";
    
            }

    Hope this helps you.

    Best Regards,

    Konstantinos Pap


    Tuesday, March 27, 2018 2:19 PM
  • You don't tell us if there can be more than 24 hours between the times. Or can there be more than 48 hours? Think about it. If the time is 2:30 in the morning and you go back in time 19 hours and fifteen minutes then what time is it? It is 16:45 the previous day, right? In other words 4:45 the previous afternoon/evening. So if the time difference can never ever be more than 24 hours then you need to somehow check to see if the second time is for the next day. Before the subtraction you could check to see if the second time is less than the first time and if it is then add a day to the second time. Or you can do as Konstantinos suggests. Or you could have a checkbox and when it is checked then that means that the second time is for the next day. But none of that will work if the time difference can be more than 48 hours. Also consider the possibility that the first time is for the last day of the month and the second time is for the first day of the next month.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, March 27, 2018 7:50 PM
  • Hello BurakTurhan,

    If you don't want the minus sign appeared, try the TimeSpan.Duration method, which will returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object. Do something changed as below.

                DateTime time1 = DateTime.ParseExact("19:15", "HH:mm", CultureInfo.CurrentCulture);
                DateTime time2 = DateTime.ParseExact("02:30", "HH:mm", CultureInfo.CurrentCulture);
    
                TimeSpan time3 = (time2 - time1).Duration();                 
                string result= $"{time3.Hours:00} : {time3.Minutes:00}";

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 28, 2018 8:10 AM
    Moderator