Function DATEDIFF_AMJ4$(ByVal dat1 As Date, Optional ByVal dat2 As Date = 0, Optional JustYear As Boolean = False)
'**************************************
'fonction DateDiffAMJ V°4
'auteur:patricktoulon 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 Year(dat2) > 9999 Or Year(dat1) < 1700 Then DATEDIFF_AMJ4$ = "yearLimite!!": Exit Function
'If Year(dat1) < 1900 Then If Year(dat1) Mod 400 = 0 Then y = 2020 Else y = 1905
'If Year(dat1) < y Then
If Year(dat1) < 1900 Then
if dat1< cdate("09/12/1760") then dat1=dat1-11
'on decale la date la plus ancienne (Dat1)à l'année +2000
yeardécalée = 2000 'Abs((Year(dat1) - y))
dat1 = DateSerial(Year(dat1) + yeardécalée, Month(dat1), Day(dat1))
dat2 = DateSerial(Year(dat2) + yeardécalée, Month(dat2), Day(dat2))
End If
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, IIf(Val(J) = 0, " et ", "") & 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_AMJ4 = Application.Trim(A & IIf(Not JustYear, " " & M & " " & et & J, ""))
End Function