Convert Difference between 2 times into Milliseconds?

后端 未结 11 2185
梦毁少年i
梦毁少年i 2020-12-06 09:11

I have two masked TextBox controls and was wondering how I\'d go about getting the time in each one and then converting the difference into milliseconds. Like, say in tb1 I

11条回答
  •  生来不讨喜
    2020-12-06 09:34

    If you are only dealing with Times and no dates you will want to only deal with TimeSpan and handle crossing over midnight.

    TimeSpan time1 = ...;  // assume TimeOfDay
    TimeSpan time2 = ...;  // assume TimeOfDay
    TimeSpan diffTime = time2 - time1;
    if (time2 < time1)  // crosses over midnight
        diffTime += TimeSpan.FromTicks(TimeSpan.TicksPerDay);
    int totalMilliSeconds = (int)diffTime.TotalMilliseconds;
    

提交回复
热议问题