Compare two dates in VB.NET [closed]

-1

I would like to know how to compare two dates with .Net; The current date and a manually inserted date.

Format: YYYY / MM / DD

    
asked by Abraham Montes 25.05.2018 в 22:46
source

2 answers

0

Friend in Google, as in SO there is a lot of information about it, you just have to try to search ...

Assuming the date format is DateTime simply take the part of the date of each one through the property Date and compare the two:

fechaActual.Date.CompareTo(fechaInsertada.Date)

See examples:

if (DateTime.Compare(fechaActual, fechaInsertada) > 0) //La fecha actual es mayor que la fecha insertada.

if (DateTime.Compare(fechaActual, fechaInsertada) == 0) //La fecha actual y la fecha insertada son iguales.

if (DateTime.Compare(fechaActual, fechaInsertada) < 0)  //La fecha actual es menor que la fecha insertada.

O:

If fechaActual.Date < fechaInsertada.Date Then
  

DateTime.CompareTo Method (DateTime)

    
answered by 25.05.2018 / 23:35
source
0
Public Function MismoDiaHora(ByVal Fecha1 As Date, ByVal Fecha2 As Date) As Boolean
        MismoDiaHora = False
        If Dia(Fecha1) = Dia(Fecha2) And _
           Mes(Fecha1) = Mes(Fecha2) And _
           Ano(Fecha1) = Ano(Fecha2) And _
           Hora(Fecha1) = Hora(Fecha2) Then
            MismoDiaHora = True
        End If
    End Function

    Public Function MismoAno(ByVal Fecha1 As Date, ByVal Fecha2 As Date) As Boolean
        MismoAno = False
        If Ano(Fecha1) = Ano(Fecha2) Then
            MismoAno = True
        End If
    End Function

    Public Function MismoMes(ByVal Fecha1 As Date, ByVal Fecha2 As Date) As Boolean
        MismoMes = False
        If Mes(Fecha1) = Mes(Fecha2) Then
            MismoMes = True
        End If
    End Function

    Public Function MismoDia(ByVal Fecha1 As Date, ByVal Fecha2 As Date) As Boolean
        MismoDia = False
        If Dia(Fecha1) = Dia(Fecha2) And _
           Mes(Fecha1) = Mes(Fecha2) And _
           Ano(Fecha1) = Ano(Fecha2) Then
            MismoDia = True
        End If
    End Function

    Public Function FechaSolo(ByVal Fecha As Date) As Date
        On Error Resume Next
        FechaSolo = Dia(Fecha) & "/" & Mes(Fecha) & "/" & Ano(Fecha)
    End Function

    Public Function ErrorDate(ByVal Fecha As String) As Boolean
        On Error Resume Next
        Dim D As Date
        D = Fecha
        If Err.Number <> 0 Then ErrorDate = True Else ErrorDate = False
    End Function

    Public Function Ano(ByVal Fecha As Date) As String
        On Error Resume Next
        Ano = Year(Fecha)
    End Function

    Public Function Dia(ByVal Fecha As Date) As String
        On Error Resume Next
        Dia = Day(Fecha)
        If Len(Dia) = 1 Then Dia = "0" & Dia
    End Function

    Public Function DiaSemana(ByVal Fecha As Date) As Integer
        On Error Resume Next
        DiaSemana = vb.Weekday(Fecha, FirstDayOfWeek.Monday)
    End Function

    Public Function Mes(ByVal Fecha As Date) As String
        On Error Resume Next
        Mes = Month(Fecha)
        If Len(Mes) = 1 Then Mes = "0" & Mes
    End Function

    Public Function Hora(ByVal Fecha As Date) As String
        On Error Resume Next
        Hora = Hour(Fecha)
        If Len(Hora) = 1 Then Hora = "0" & Hora
    End Function

    Public Function Minuto(ByVal Fecha As Date) As String
        On Error Resume Next
        Minuto = Minute(Fecha)
        If Len(Minuto) = 1 Then Minuto = "0" & Minuto
    End Function

    Public Function Segundos(ByVal Fecha As Date) As String
        On Error Resume Next
        Segundos = Second(Fecha)
        If Len(Segundos) = 1 Then Segundos = "0" & Segundos
    End Function
    
answered by 28.05.2018 в 00:42