ciao Aurelio,
in un modulo standard copia una di queste due funzioni :
Public Function YMDgone(ByVal argdate As Date)
' shows difference between argDate and today
' by Karl Donaubauer 24.1.1999 using the DevAshishian boolean-trick ;-)
Dim Ygone As Integer, Mgone As Byte, Dgone As Byte
If IsNull(argdate) Then Exit Function
Ygone = DateDiff("yyyy", argdate, Date) _
- Int(Format(Date, "mmdd") < Format(argdate, "mmdd"))
Mgone = Int((DateDiff("m", argdate, Date) _
- Int(Format(Date, "dd") < Format(argdate, "dd")))) Mod 12
If Day(argdate) <= Day(Date) Then
Dgone = Format(Date, "dd") - Format(argdate, "dd")
Else
Dgone = Day(DateSerial(Year(argdate), Month(argdate) + 1, 0)) _
- Day(argdate) + Day(Date)
End If
YMDgone = Ygone & " year" & IIf(Ygone = 1, " ", "s ") _
& Mgone & " month" & IIf(Mgone = 1, " ", "s ") _
& Dgone & " day" & IIf(Dgone = 1, " ", "s")
End Function
Public Function fAge(dteStart As Variant, dteEnd As Variant) As Variant
'*******************************************
'Purpose: Accurately return the difference
' between two dates, expressed as
' years.months.days
'Coded by: raskew (from MS Access forum)
'Inputs: From debug (immediate) window
' 1) ? fAge(#12/1/1950#, #8/31/2006#) 'Calculate btw 2 specific dates
' 2) ? fAge(#12/30/2005#, Date()) ' Calculate as of today's date
'*******************************************
Dim intHold As Integer
Dim dayhold As Integer
'correctly return number of whole months difference
'the (Day(dteEnd) < Day(dteStart)) is a Boolean statement
'that returns -1 if true, 0 if false
intHold = DateDiff("m", dteStart, dteEnd) + (Day(dteEnd) < Day(dteStart))
'correctly return number of days difference
If Day(dteEnd) < Day(dteStart) Then
dayhold = DateDiff("d", dteStart, DateSerial(Year(dteStart), Month(dteStart) + 1, 0)) + Day(dteEnd)
Else
dayhold = Day(dteEnd) - Day(dteStart)
End If
fAge = LTrim(Str(intHold \ 12)) & " years " & LTrim(Str(intHold Mod 12)) & " months " & LTrim(Str(dayhold)) & " days"
End Function
presa da qui :https://www.devhut.net/2010/06/22/ms-access-calculate-the-age/
la prima è di Karl Donaubauer [Access MVP].
la prima la richiami dalla finestra immediata in questo modo :
? YMDgone(#1991/08/25#)
25 years 7 months 3 days
la seconda:
? fAge(#1991/08/25#,date())
25 years 7 months 3 days
per sommare 4 anni alla data a 18/05/1994 :
? dateadd("yyyy",4,#1994/05/18#)
18/05/1998.
ciao, Sandro.
[ edit ]
mi sembrava di avere realizzato qualcosa di questo tipo di mio :
Public Function myDate(mydate1 As Variant, _
mydate2 As Variant) As String
If IsNull(mydate1) Or IsNull(mydate2) Then Exit Function
Dim lngYear As Long
Dim lngMonth As Long
Dim lngDays As Long
Dim strDate As String
lngYear = DateDiff("yyyy", mydate1, mydate2) - IIf(Format$([mydate1], "mmdd") > Format$([mydate2], "mmdd"), 1, 0)
mydate1 = DateAdd("yyyy", lngYear, mydate1)
lngMonth = DateDiff("m", mydate1, mydate2) - IIf(Format$([mydate1]) > Format$([mydate2]), 1, 0)
mydate1 = DateAdd("m", lngMonth, mydate1)
lngDays = DateDiff("d", mydate1, mydate2)
If lngYear > 0 Then
strDate = str(lngYear) & Chr(32) & IIf(lngYear = 1, "anno", "anni") & Chr(32)
End If
If lngMonth > 0 Then
strDate = strDate & lngMonth & Chr(32) & IIf(lngMonth = 1, "mese", "mesi") & Chr(32)
End If
If lngDays > 0 Then
strDate = strDate & lngDays & Chr(32) & IIf(lngDays = 1, "giorno", "giorni")
End If
myDate = strDate
End Function
la richiami così :
? myDate(#1991/08/25#,#2017/03/28#)
25 anni 7 mesi 3 giorni
buono studio
[end edit]