
Restar horas
Publicado por Kenneth (15 intervenciones) el 20/10/2021 22:31:50
Como puedo restar un datetimenow con un textbox que igual sera una fecha?
Valora esta pregunta


0












Dim D1 As Date = "22:50:00"
Dim D2 As Date = "22:40:00"
Dim Diferencia As Integer = DateAndTime.DateDiff(DateInterval.Minute, D2, D1)
MessageBox.Show(Diferencia)








Dim D1 As Date = DateTime.Now.ToLongTimeString
Dim D2 As Date = TxtHora.Text
Dim Diferencia As Integer = DateAndTime.DateDiff(DateInterval.Minute, D2, D1)
MessageBox.Show(Diferencia)







' La Function que hace el formatoPrivate Function SegundosFormateadosHoras(ByVal CuantidadSegundos As Integer) As String
Dim Resultado As String = ""
Dim Tmp As Integer
Tmp = CuantidadSegundos \ 3600 ' Calcular horas
Resultado = String.Format("{0:D2}", Tmp) & ":"
CuantidadSegundos -= Tmp * 3600
Tmp = CuantidadSegundos \ 60 ' Calcular minutos
Resultado &= String.Format("{0:D2}", Tmp) & ":"
CuantidadSegundos -= Tmp * 60
Tmp = CuantidadSegundos ' Calcular segundos
Resultado &= String.Format("{0:D2}", Tmp)
Return Resultado
End Function
' Tus codigos que llaman el FunctionDim D1 As Date = DateTime.Now.ToLongTimeString
Dim D2 As Date = TxtHora.Text
Dim Diferencia As Integer = DateAndTime.DateDiff(DateInterval.Second, D2, D1) ' !!! Trabajar con SECOND
MessageBox.Show(SegundosFormateadosHoras(Diferencia)) ' !!! Llamar la Function






Dim D1 As Date = DateTime.Now.ToLongTimeString
On Error Resume Next
Dim D2 As Date = TXTHORA.Text
On Error Resume Next
Dim Diferencia As Integer = DateAndTime.DateDiff(DateInterval.Second, D2, D1) ' !!! Trabajar con SECOND
LBRESUL.Text = (SegundosFormateadosHoras(Diferencia))
Timer1.Enabled = True
LBACTUAL.Text = D1
Dim thishour = Hour((SegundosFormateadosHoras(Diferencia)))
Dim thishour1 = Minute((SegundosFormateadosHoras(Diferencia)))
Dim hor1 = thishour * 60
horalabel.Text = hor1
minutoslabel.Text = thishour1
tiemposlabel.Text = hor1 + thishour1
































' ... ... ... ' *** Esta es el calculo iníciale (PHR) : En el reloj, D2 es mas grande que D1 'If SegundosD2 < SegundosD1 Then ' Para hacer calcul correcto sobre hora : tras 24h, viene 00h. ' SegundosD2 += 86400 ' 24 * 3600, es decir 24 horas 'End If 'Dim Diferencia As Integer = SegundosD2 - SegundosD1 ' *** Esta es el calculo pedido (Kenneth) : En el reloj, D1 es mas grande que D2If SegundosD1 < SegundosD2 Then ' Para hacer calcul correcto sobre hora : tras 24h, viene 00h.
SegundosD1 += 86400 ' 24 * 3600, es decir 24 horas
End If
Dim Diferencia As Integer = SegundosD1 - SegundosD2
' ... ... ...Public Class FContadorTiempo
Dim D1 As Date
Dim D2 As Date
Dim Diferencia As Integer
Private Sub FBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TBTiempoD1.Text = Now.ToLongTimeString ' Tiempo del sistema por defecto
End Sub
Private Function SegundosFormateadosHoras(ByVal CuantidadSegundos As Integer) As String
Dim Resultado As String = ""
Dim Tmp As Integer
Tmp = CuantidadSegundos \ 3600 ' Calcular horas
Resultado = String.Format("{0:D2}", Tmp) & ":"
CuantidadSegundos -= Tmp * 3600
Tmp = CuantidadSegundos \ 60 ' Calcular minutos
Resultado &= String.Format("{0:D2}", Tmp) & ":"
CuantidadSegundos -= Tmp * 60
Tmp = CuantidadSegundos ' Calcular segundos
Resultado &= String.Format("{0:D2}", Tmp)
Return Resultado
End Function
Private Function CuantidadSegundosEnTiempo(ByVal D As Date) As Integer ' Sumar las segundos sin mirar la fecha
Dim TiempoSinFecha As String = D.ToLongTimeString
Dim PrimerDoblePunto As Integer = TiempoSinFecha.IndexOf(":", 0)
Dim H As Integer = CType(TiempoSinFecha.Substring(0, PrimerDoblePunto), Integer)
Dim M As Integer = CType(TiempoSinFecha.Substring(PrimerDoblePunto + 1, 2), Integer)
Dim S As Integer = CType(TiempoSinFecha.Substring(PrimerDoblePunto + 4), Integer)
Return S + M * 60 + H * 3600
End Function
Private Function RedondearLosSegundosAlTiempo(ByVal TiempoEscritoConSegungos As Integer) As Integer
Dim SegundosDelTiempo As Integer = TiempoEscritoConSegungos Mod 60 If SegundosDelTiempo < 30 ThenReturn TiempoEscritoConSegungos - SegundosDelTiempo ' Si menos que 30'', borrar los segundos
Else
Return TiempoEscritoConSegungos + (60 - SegundosDelTiempo) ' Si 30'' o mas, pasa a el minuto superior
End If
End Function
Private Sub BHoraSistema_Click(sender As Object, e As EventArgs) Handles BHoraSistema.Click
TBTiempoD1.Text = Now.ToLongTimeString ' poner el tiempo del sistema si me gusta
End Sub
Private Sub BRestar_Click(sender As Object, e As EventArgs) Handles BRestar.Click
' D1 y D2 estan de tipo Date, contienne una fecha con la hora. Cuando D2 pasa el día, el sistema toma el día siguiente y hace dificules los calculos : DateDiff mira todo fecha + hora.. ' Es la razón porque calculamos la soma de segundos de cada tiempo. Después, podemos restar los segundos independientemente del fecha.Dim SegundosD1 As Integer
Dim SegundosD2 As Integer
If TBTiempoD1.Text = String.Empty Or TBTiempoD2.Text = String.Empty Then ' No continuar si no los datos correctos
Exit Sub
End If
D1 = CType(TBTiempoD1.Text, Date)
D2 = CType(TBTiempoD2.Text, Date)
SegundosD1 = CuantidadSegundosEnTiempo(D1)
SegundosD2 = CuantidadSegundosEnTiempo(D2)
'' Esta es mi calculo iniciale (PHR) : En el relog, D2 es mas grande que D1 ''If SegundosD2 < SegundosD1 Then ' Para hacer calcul correcto sobre hora : tras 24h, viene 00h. '' SegundosD2 += 86400 ' 24 * 3600, es decir 24 horas ''End If ''Dim Diferencia As Integer = SegundosD2 - SegundosD1 '' Esta es el calculo pedido (Kenneth) : En el relog, D1 es mas grande que D2If SegundosD1 < SegundosD2 Then ' Para hacer calcul correcto sobre hora : tras 24h, viene 00h.
SegundosD1 += 86400 ' 24 * 3600, es decir 24 horas
End If
Dim Diferencia As Integer = SegundosD1 - SegundosD2
' Si quieres quitar los segundos, redondear los segundos : Si igual 30' o mas, pasa al minuto superior, si no, borrar los segundosDiferencia = RedondearLosSegundosAlTiempo(Diferencia)
TBResultado.Text = SegundosFormateadosHoras(Diferencia)
MostarTextBoxHMS()
End Sub
Private Sub MostarTextBoxHMS()
Dim PrimerDoblePunto As Integer = TBResultado.Text.IndexOf(":", 0)
TBHoraResultado.Text = TBResultado.Text.Substring(0, PrimerDoblePunto)
TBMinutoResultado.Text = TBResultado.Text.Substring(PrimerDoblePunto + 1, 2)
TBSegundoResultado.Text = TBResultado.Text.Substring(PrimerDoblePunto + 4)
End Sub
End Class





' Para calcular SIN segundosSegundosD1 = (CuantidadSegundosEnTiempo(D1) \ 60) * 60 ' Atención : división entera \ NO escribir estas líneas si calculo con segundos
SegundosD2 = (CuantidadSegundosEnTiempo(D2) \ 60) * 60
Private Sub MostarTextBoxHMS()
Dim PrimerDoblePunto As Integer = TBResultado.Text.IndexOf(":", 0)
TBHoraResultado.Text = TBResultado.Text.Substring(0, PrimerDoblePunto)
TBMinutoResultado.Text = TBResultado.Text.Substring(PrimerDoblePunto + 1, 2)
' Para mostrar los segundos de los calculos, con redondeado o noTBSegundoResultado.Text = TBResultado.Text.Substring(PrimerDoblePunto + 4)
' Para mostrar los segundos siempre egual a 00TBSegundoResultado.Text = "00"
' Para no mostrar los segundos, quitar el TextBox TBSegundoResultado ' Nada a escribir aquiEnd Sub
' Para redondear los segundos segun que estan igual a 30" o noDiferencia = RedondearLosSegundosAlTiempo(Diferencia)
' Para NO redondear los segundos ' Diferencia = RedondearLosSegundosAlTiempo(Diferencia) ' esta linea en comentario, nada a escribir aquíPrivate Sub BRestar_Click(sender As Object, e As EventArgs) Handles BRestar.Click
' D1 y D2 estan de tipo Date, contienne una fecha con la hora. Cuando D2 pasa el día, el sistema toma el día siguiente y hace dificules los calculos : DateDiff mira todo fecha + hora.. ' Es la razon porque calculamos la soma de segundos de cada tiempo. Después, podemos restar los segundos independientemente del fecha.Dim SegundosD1 As Integer
Dim SegundosD2 As Integer
If TBTiempoD1.Text = String.Empty Or TBTiempoD2.Text = String.Empty Then ' No continuar si no los datos correctos
Exit Sub
End If
D1 = CType(TBTiempoD1.Text, Date)
D2 = CType(TBTiempoD2.Text, Date)
SegundosD1 = CuantidadSegundosEnTiempo(D1)
SegundosD2 = CuantidadSegundosEnTiempo(D2)
' AQUÍ, DEBES ELEGIR : ' Para calcular SIN segundosSegundosD1 = (CuantidadSegundosEnTiempo(D1) \ 60) * 60 ' Atencion : division entera \ NO escribir estas lineas si calculo con segundos
SegundosD2 = (CuantidadSegundosEnTiempo(D2) \ 60) * 60
'' Esta es mi calculo iniciale (PHR) : En el relog, D2 es mas grande que D1 ''If SegundosD2 < SegundosD1 Then ' Para hacer calcul correcto sobre hora : tras 24h, viene 00h. '' SegundosD2 += 86400 ' 24 * 3600, es decir 24 horas ''End If ''Dim Diferencia As Integer = SegundosD2 - SegundosD1 '' Esta es el calculo pedido (Kenneth) : En el relog, D1 es mas grande que D2If SegundosD1 < SegundosD2 Then ' Para hacer calcul correcto sobre hora : tras 24h, viene 00h.
SegundosD1 += 86400 ' 24 * 3600, es decir 24 horas
End If
Dim Diferencia As Integer = SegundosD1 - SegundosD2
' AQUÍ, DEBES ELEGIR : ' Para redondear los segundos segun que estan igual a 30" o noDiferencia = RedondearLosSegundosAlTiempo(Diferencia)
' Para NO redondear los segundos ' Diferencia = RedondearLosSegundosAlTiempo(Diferencia) ' esta linea en comentario, nada a escribir aquiTBResultado.Text = SegundosFormateadosHoras(Diferencia)
MostarTextBoxHMS()
End Sub
Private Sub MostarTextBoxHMS()
Dim PrimerDoblePunto As Integer = TBResultado.Text.IndexOf(":", 0)
TBHoraResultado.Text = TBResultado.Text.Substring(0, PrimerDoblePunto)
TBMinutoResultado.Text = TBResultado.Text.Substring(PrimerDoblePunto + 1, 2)
' AQUÍ, DEBES ELEGIR : ' Para mostrar los segundos de los calculos, con redondeado o noTBSegundoResultado.Text = TBResultado.Text.Substring(PrimerDoblePunto + 4)
' Para mostrar los segundos siempre egual a 00TBSegundoResultado.Text = "00"
' Para no mostrar los segundos, quitar el TextBox TBSegundoResultado ' Nada a escribir aquiEnd Sub




