1

I'm getting values from two dateTimePickers and I need to get the time difference between them. Now, I'm calculating the difference using the below code, but I'm getting a wrong time difference with Milliseconds. Is there a way to calculate the time difference without considering Milliseconds. WindowsFormScreenshot

TimeSpan difference = dateTimePicker2.Value - dateTimePicker1.Value;

Example 1;

  • Start time : 4/24/2022 2:30:11 PM
  • end time : 4/24/2022 2:30:11 PM
  • Actual difference : -00:00:00.0049863
  • Expected difference : 00:00:00

Example 2;

  • Start time : 4/24/2022 2:29:01 PM

  • end time : 4/24/2022 10:29:01 PM

  • Actual difference : 07:59:59.9037925

  • Expected difference : 08:00:00

Example 3;

  • Start time : 4/24/2022 4:24:28 PM

  • end time : 4/24/2022 10:30:28 PM

  • Actual difference : 06:05:59.9648821

  • Expected difference : 06:06:00

It will give expected answers, when I'm retyping the time value in both dateTimePickers in Winform but doesn't work when I didn't retype minutes field in each dateTimePicker.

I don't want to round up the answers. I want to do the difference calculation without considering milliseconds. So that it will give correct answers for what ever the values we parse for dateTimePickers.

(I'm a newbie to c#. so I'm sorry if I'm not much clear. Thank you so much!)

  • Thank you for the suggestion. However, I want the exact time difference without considering Milliseconds. So rounding up don't work. – Janani Balasooriya Apr 24 '22 at 09:50
  • You said you don't want to round up, but your second example is rounding up? – Martheen Apr 24 '22 at 10:20
  • I need the time difference with exact hours : minutes: seconds but without considering milliseconds. – Janani Balasooriya Apr 24 '22 at 10:54
  • [Custom TimeSpan format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings?WT.mc_id=DT-MVP-5003235) – Reza Aghaei Apr 24 '22 at 11:17
  • 1
    I'm 99% sure you're intializing one or both of the datetime pickers with DateTime.Now (or something similar) which is the current time INCLUDING milliseconds (even though the picker won't show it). Maybe you're then manually changing the input to some other time (but that keeps the milliseconds in tact). THAT's where the differences in milliseconds comes from. THERE'S your problem. Get rid of the milliseconds there and then your calculations will result in round seconds too, no need for rounding. – RobIII Apr 24 '22 at 12:15
  • 1
    So do this: `var t = DateTime.Now; var now = new DateTime(t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second);` and then initialise your DateTimePickers to `now`. – RobIII Apr 24 '22 at 12:23
  • Yes!!! It is 100% correct. Thank you so much @RobIII . I have initialized DateTimePickers as in your code. Then, it worked correctly as expected. – Janani Balasooriya Apr 24 '22 at 15:29
  • @JananiBalasooriya It doesn't work as expected if you don't round the result. If you ignore milliseconds, the result might be 1 seconds off. – Reza Aghaei Apr 24 '22 at 16:09
  • 1
    @RezaAghaei No because Janani uses [two DateTimePickers](https://i.stack.imgur.com/EIJa0.png), each not displaying milliseconds. – RobIII Apr 25 '22 at 08:30

3 Answers3

2
   DateTime adjustedDateTimePicker2 = dateTimePicker2.Value.AddMilliseconds(dateTimePicker2.Value.Millisecond * -1);
        
   DateTime adjustedDateTimePicker1 = DateTimePicker.Value.AddMilliseconds(dateTimePicker.Value.Millisecond * -1);
        
   TimeSpan difference = adjustedDateTimePicker2 - adjustedDateTimePicker1;
Morten Bork
  • 1,413
  • 11
  • 23
0

You may read hours, minutes, and seconds separately:

int hourCnt = difference.Hours;
int minuteCnt = diffenrence.Minutes;
int secondCnt = difference.Seconds;
Elec1
  • 235
  • 2
  • 8
  • Thank you! I have tried your method but it isn't giving the expected answer. If we consider above **Example 2**, it will give 'hourCnt' of 7 and not the expected answer of 8. – Janani Balasooriya Apr 24 '22 at 09:40
  • Janani Balasooriya: you add 500 ms to your difference value you should get the values to do expect. Values which differ less then half a second from your expected result will then be rounded up. – Elec1 Apr 24 '22 at 10:18
0

You can first round the timespan, then format the result using a Custom Timespan format string like "hh':'mm':'ss" or @"hh\:mm\:ss":

var d1 = DateTime.Parse("4/24/2022 4:24:28 PM");
var d2 = DateTime.Parse("4/24/2022 10:30:28 PM");
var d = d2 - d1;

int precision = 0;
const int TIMESPAN_SIZE = 7;
int factor = (int)Math.Pow(10, (TIMESPAN_SIZE - precision));
TimeSpan roundedTimeSpan = 
    new TimeSpan(((long)Math.Round((1.0 * d.Ticks / factor)) * factor));

MessageBox.Show(roundedTimeSpan.ToString(@"hh':'mm':'ss"));

Which shows:

06:06:00
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398