Dim date1 As Date, date2 As Date date1 = "01/01/2004" date2 = "12/08/2005" MsgBox "Durée en jours : " & DateDiff("d", date1, date2) MsgBox "Durée en nombre de mois : " & DateDiff("m", date1, date2) MsgBox "Durée en années : " & DateDiff("y", date1, date2)
Public Function Age1(D1 As String, D2 As String) As String Age1 = "une date est manquante" If D1 = "" Or D2 = "" Then Exit Function Age1 = "La 2ème date doit nécessairement être plus grande que la 1ère !" If DateValue(D1) > DateValue(D2) Or Not IsDate(D2) Then Exit Function Dim toto As String, a As Integer, m As Integer, j As Integer toto = DateValue(DateSerial(Year(D2) - Year(D1), Month(D2) - Month(D1), Day(D2) - Day(D1))) a = Val(Format(toto, "yy")) m = Val(Format(toto, "mm")) j = Val(Format(toto, "dd")) Age1 = Str(a) & " an" & IIf(a > 1, "s, ", ", ") & Str(m) & " mois et " _ & Str(j) & " jour" & IIf(j > 1, "s", "") End Function
duree = Age1(date1, date2)
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub Command1_Click() MsgBox Age1(date_debut, date_fin) End Sub Public Function Age1(D1 As String, D2 As String) As String Age1 = "une date est manquante" If D1 = "" Or D2 = "" Then Exit Function Age1 = "La 2ème date doit nécessairement être plus grande que la 1ère !" If DateValue(D1) > DateValue(D2) Or Not IsDate(D2) Then Exit Function Dim toto As String, a As Integer, m As Integer, j As Integer toto = DateValue(DateSerial(Year(D2) - Year(D1), Month(D2) - Month(D1), Day(D2) - Day(D1))) a = Val(Format(toto, "yy")) m = Val(Format(toto, "mm")) j = Val(Format(toto, "dd")) Age1 = Str(a) & " an" & IIf(a > 1, "s, ", ", ") & Str(m) & " mois et " _ & Str(j) & " jour" & IIf(j > 1, "s", "") End Function
Public Function Age(D1 As String, D2 As String) As String Dim nba As Integer, nbm As Integer, nbj As Integer Age = "une date est manquante" If D1 = "" Or D2 = "" Then Exit Function Age = "La 2ème date doit nécessairement être plus grande que la 1ère !" If DateValue(D1) > DateValue(D2) Or Not IsDate(D2) Then Exit Function Dim LaFeinte As Long LaFeinte = IIf(Day(D2) < Day(D1), 1, 0) nba = Year(D2) - Year(D1) - Switch(Month(D2) < Month(D1), 1, Month(D2) = Month(D1), _ LaFeinte, True, 0) nbm = (Month(D2) - Month(D1) - LaFeinte + 12) Mod 12 nbj = Day(D2) - Day(D1) + LaFeinte * Day(DateSerial(Year(D2), Month(D2), 1) - 1) Age = Trim(IIf(nba > 0, nba & " an" & IIf(nba > 1, "s", "") & " ", "") & _ IIf(nbm > 0, nbm & " mois ", "") & IIf((nbj > 0) Or (nba + nbm = 0), nbj & " jour" & _ IIf(nbj > 1, "s", ""), "")) End Function