Compracion entre 2 fechas
Publicado por Irvin (1 intervención) el 20/11/2008 03:34:35
Esta no es una pregunta, mas que nada es una respuesta a una problema que tenia en un sistema que validaba un rango de 2 fechas, es decir 4 fechas. Fecha 1 Inicial-Final , Fecha2 Inicial-Final. Entonces tiene que comparar si las fechas se cruzan o no. Por ejemplo entre la fecha1 10/03/08-20/03/08 y la fecha 2 08/03/08-15/03/08, se cruzan porque la fecha2 está agarrando los días 10 al 15 de la fecha1.
Bueno aquí está el código.
Dim fecha1, fecha2, fecha3, fecha4 As Date
Dim diafe12, diafe13, diafe34 As Integer
fecha1 = Format("dd/MM/yyyy", CDate(TextBox1.Text))
fecha2 = Format("dd/MM/yyyy", CDate(TextBox2.Text))
fecha3 = Format("dd/MM/yyyy", CDate(TextBox3.Text))
fecha4 = Format("dd/MM/yyyy", CDate(TextBox4.Text))
diafe12 = DateDiff(DateInterval.Day, fecha1, fecha2)
diafe13 = DateDiff(DateInterval.Day, fecha1, fecha3)
diafe34 = DateDiff(DateInterval.Day, fecha3, fecha4)
If diafe12 < 0 Then diafe12 *= -1
If diafe13 < 0 Then diafe13 *= -1
If diafe34 < 0 Then diafe34 *= -1
If diafe12 >= diafe13 And Date.Compare(fecha1, fecha3) = -1 Then
TextBox5.Text = "Se cruzan las fechas"
ElseIf diafe34 >= diafe13 And Date.Compare(fecha1, fecha3) = 1 Then
TextBox5.Text = "Se cruzan las fechas"
Else
TextBox5.Text = "No se cruzan las fechas"
End If
Espero que les haya ayudado. Se ven que se puede mejorar el código, anexenlo no se queden con el código.
Bueno aquí está el código.
Dim fecha1, fecha2, fecha3, fecha4 As Date
Dim diafe12, diafe13, diafe34 As Integer
fecha1 = Format("dd/MM/yyyy", CDate(TextBox1.Text))
fecha2 = Format("dd/MM/yyyy", CDate(TextBox2.Text))
fecha3 = Format("dd/MM/yyyy", CDate(TextBox3.Text))
fecha4 = Format("dd/MM/yyyy", CDate(TextBox4.Text))
diafe12 = DateDiff(DateInterval.Day, fecha1, fecha2)
diafe13 = DateDiff(DateInterval.Day, fecha1, fecha3)
diafe34 = DateDiff(DateInterval.Day, fecha3, fecha4)
If diafe12 < 0 Then diafe12 *= -1
If diafe13 < 0 Then diafe13 *= -1
If diafe34 < 0 Then diafe34 *= -1
If diafe12 >= diafe13 And Date.Compare(fecha1, fecha3) = -1 Then
TextBox5.Text = "Se cruzan las fechas"
ElseIf diafe34 >= diafe13 And Date.Compare(fecha1, fecha3) = 1 Then
TextBox5.Text = "Se cruzan las fechas"
Else
TextBox5.Text = "No se cruzan las fechas"
End If
Espero que les haya ayudado. Se ven que se puede mejorar el código, anexenlo no se queden con el código.
Valora esta pregunta


0