Obtain the minute difference between the values of 2 DateTimePicker controls [closed]

1

I try to get the minute difference in TextBox with two controls DateTimePicker .

When compiling the program for the first time it shows me a very long number, then when changing the time in the control, it does show me the time.

This code is being executed in the event ValueChanged of each DateTimePicker . That is, it is found as such in each of the 2 events, but it is strange because it only happens when I execute it initially.

DateTime horaInicio = dtpHorainicial.Value;
DateTime horaFinal = dtpHorafinal.Value;

// A la hora final le restamos la hora de inicio
TimeSpan diferencia = horaFinal.Subtract(horaInicio);

// Mostramos la diferencia en un control TextBox
txt_tiempoTotal.Text = $"{diferencia.TotalMinutes} Minutos";
    
asked by Mauricio Fernández 15.05.2017 в 05:42
source

1 answer

0

Do you want the time difference between the two, expressed in minutes ?, or do you want the difference between the minutes of one datetimepicker and the other?

Example:

  • dtp1: 10: 30hs
  • dtp2: 0: 30hs

The difference in minutes is 600; while the difference between the minutes of each dtp is 0.

If it is the first option, I would follow the comment made to you, to verify the values that are reaching the variables horaInicio and horaFinal .

If it's the second option, then you can do this calculation:

int diferencia = horaFinal.Minute - horaInicio.Minute

(if it gives you a negative value, the difference is in favor of horaInicio ) .

    
answered by 15.05.2017 в 18:44