Getting time span between two times in C#?

后端 未结 4 518
有刺的猬
有刺的猬 2020-11-30 01:22

I have two textboxes. One for a clock in time and one for clock out. The times will be put in this format:

Hours:Minutes

Lets say I have cl

相关标签:
4条回答
  • 2020-11-30 02:11

    You could use the TimeSpan constructor which takes a long for Ticks:

     TimeSpan duration = new TimeSpan(endtime.Ticks - startTime.Ticks);
    
    0 讨论(0)
  • 2020-11-30 02:12

    Two points:

    1. Check your inputs. I can't imagine a situation where you'd get 2 hours by subtracting the time values you're talking about. If I do this:

          DateTime startTime = Convert.ToDateTime("7:00 AM");
          DateTime endtime = Convert.ToDateTime("2:00 PM");
          TimeSpan duration = startTime - endtime;
      

      ... I get -07:00:00 as the result. And even if I forget to provide the AM/PM value:

          DateTime startTime = Convert.ToDateTime("7:00");
          DateTime endtime = Convert.ToDateTime("2:00");
          TimeSpan duration = startTime - endtime;
      

      ... I get 05:00:00. So either your inputs don't contain the values you have listed or you are in a machine environment where they are begin parsed in an unexpected way. Or you're not actually getting the results you are reporting.

    2. To find the difference between a start and end time, you need to do endTime - startTime, not the other way around.

    0 讨论(0)
  • 2020-11-30 02:21
    string startTime = "7:00 AM";
    string endTime = "2:00 PM";
    
    TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
    
    Console.WriteLine(duration);
    Console.ReadKey();
    

    Will output: 07:00:00.

    It also works if the user input military time:

    string startTime = "7:00";
    string endTime = "14:00";
    
    TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
    
    Console.WriteLine(duration);
    Console.ReadKey();
    

    Outputs: 07:00:00.

    To change the format: duration.ToString(@"hh\:mm")

    More info at: http://msdn.microsoft.com/en-us/library/ee372287.aspx

    Addendum:

    Over the years it has somewhat bothered me that this is the most popular answer I have ever given; the original answer never actually explained why the OP's code didn't work despite the fact that it is perfectly valid. The only reason it gets so many votes is because the post comes up on Google when people search for a combination of the terms "C#", "timespan", and "between".

    0 讨论(0)
  • 2020-11-30 02:21

    Another way ( longer ) In VB.net [ Say 2300 Start and 0700 Finish next day ]

    If tsStart > tsFinish Then

                                ' Take Hours difference and adjust accordingly
                                tsDifference = New TimeSpan((24 - tsStart.Hours) + tsFinish.Hours, 0, 0)
    
                                ' Add Minutes to Difference
                                tsDifference = tsDifference.Add(New TimeSpan(0, Math.Abs(tsStart.Minutes - tsFinish.Minutes), 0))
    
    
                                ' Add Seonds to Difference
                                tsDifference = tsDifference.Add(New TimeSpan(0, 0, Math.Abs(tsStart.Seconds - tsFinish.Seconds)))
    
    0 讨论(0)
提交回复
热议问题