Número de días, meses y años entre 2 fechas

Supongamos que tenemos 2 calendarios y en cada uno de ellos llamaremos las fechas.

'calcula días

Dim fecha1 As Date = Me.Calendar1.SelectedDate '01/10/2012
Dim fecha2 As Date = Me.Calendar2.SelectedDate '07/10/2012
Dim diasa As Integer = DateDiff(DateInterval.Day, fecha1, fecha2)


'calcula meses
Dim fecha1 As Date = Me.Calendar1.SelectedDate '01/10/2012
Dim fecha2 As Date = Me.Calendar2.SelectedDate '07/12/2012
Dim diasa As Integer = DateDiff(DateInterval.Month, fecha1, fecha2)

'calcula años
Dim fecha1 As Date = Me.Calendar1.SelectedDate '01/10/2012
Dim fecha2 As Date = Me.Calendar2.SelectedDate '07/11/2014
Dim diasa As Integer = DateDiff(DateInterval.Year, fecha1, fecha2)

Publicar un comentario

2 Comentarios

  1. la forma de saber exacto los años mese i días es así

    Sub anosMesesDias(fechaInicio As Date, fechaFin As Date)

    anos = fechaFin.Year - fechaInicio.Year

    If fechaInicio.Month > fechaFin.Month Then anos = anos - 1

    If fechaFin.Month < fechaInicio.Month Then meses = 12 - fechaInicio.Month + fechaFin.Month Else meses = fechaFin.Month - fechaInicio.Month

    If fechaFin.Day < fechaInicio.Day Then
    meses = meses - 1
    If fechaFin.Month = fechaInicio.Month Then anos = anos - 1 : meses = 11
    End If

    dias = fechaFin.Day - fechaInicio.Day

    If dias < 0 Then
    m = CInt(fechaFin.Month) - 1
    If m = 0 Then m = 12

    tope = DateTime.DaysInMonth(fechaFin.Year, m) 'averigua meses de 28 29 30 i 31 dias
    dias = tope + dias

    End If

    End Sub

    ResponderBorrar