Const year400 As Long = 2000
Function DATEDIFF_AMJ5$(ByVal dat1 As Date, Optional ByVal dat2 As Date = 0, Optional JustYear As Boolean = False)
'**************************************
'fonction DateDiffAMJ V°5
'auteur:patricktoulon avec Eriiic sur Exceldownloads
'date de mise en jour V°4:04/07/2021
' mise a jour supplementaire
' ajout de l'argument boolean "JustYear" pour ne récuprérer que les années
'*************************************
Dim A$, M$, J$, Dtemp$, et$, yeardécalée&, y
If dat2 = 0 Then dat2 = Date
If dat1 > dat2 Then Dtemp = dat2: dat2 = dat1: dat1 = Dtemp
If dat1 <= CDate("10/12/1582") Then dat1 = dat1 + IIf(Month(dat1) > 11, 10, 1)
If dat2 <= CDate("10/12/1582") Then dat2 = dat2 + IIf(Month(dat1) > 11, 10, 1)
If dat2 > CDate("09/12/1582") And dat2 < CDate("31/12/1582") Then dat2 = dat2 + IIf(Month(dat1) = 12, -10, 1)
dat1 = DateSerial(Year(dat1) + year400, Month(dat1), Day(dat1))
dat2 = DateSerial(Year(dat2) + year400, Month(dat2), Day(dat2))
A = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""y"")")
M = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""ym"")")
J = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""md"")")
A = IIf(A = 0, "", IIf(A = 1, A & " an", A & " ans"))
M = IIf(M = 0, "", IIf(M >= 1, M & " mois", M & " mois"))
J = IIf(J = 0, "", IIf(J = 1, "1 jour", J & " jours"))
et = IIf(Val(A) > 0 Or Val(M) > 0, IIf(Val(J) > 0, " et ", " "), "")
DATEDIFF_AMJ5 = Application.Trim(A & IIf(Not JustYear, " " & M & " " & et & J, ""))
End Function