Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Date of Birth to Age
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 8
Divers
Thread ID:
01045983
Message ID:
01045993
Vues:
21
Jon

>How can I convert a Date of Birth to an age?


this class from me has german comments, but I'm sure it gets you going

It also takes care of the fact that there *are* people born on the 29th of february
the property lDateBack allows to set whether You want it to be the 28th on non leap-years
or the 1st of march
define class Birthday as relation
    dRefDate    = date()    && Auf welches Datum soll der Geburtstag
                            && bezogen sein
    
    dBirthDate    = {}        && Datum der Geburt
    
    dBirthDay    = {}        && nächster Geburtstag bezogen auf das RefDate
                            
    lDateBack    = .F.        && In einem NICHTSchaltjahr auf den 28. springen,
                            && wenn der Geburtstag am 29.02. ist
    

    *=========================================================
    *
    *         PROCEDURE: NextBirthday             
    *
    *=========================================================
    PROCEDURE NextBirthday             
    *  Created...........:  1.August 2003, 11:23 Uhr
    *  Changed...........:   
    *  Description.......:  Datum des nächsten Geburtstags zurückliefern
    *  Calling Samples...:  ? NextBirthday(<ExpD>)
    *  Parameters........:  tdDate
    *  Returns...........:  Date
    lparameters    tdBirthDate, tdRefDate, tlBack
    tdBirthDate         = iif(vartype(tdBirthDate)     = "D", tdBirthDate, This.dBirthDate)    
    tdRefDate     = iif(vartype(tdRefDate)= "D", tdRefDate, This.dRefDate)
    tlBack         = iif(parameters()=3, tlBack, This.lDateBack)


    This.dRefDate     = tdRefDate
    This.dBirthDate = tdBirthDate
    This.lDateBack    = tlBack

    LOCAL ldRetVal, ldX, lnDay, lnMonth

    ldRetval = tdBirthDate

    if ! empty(ldRetVal)

        ldRetVal = This.DateToRef( tdBirthDate, tdRefDate, tlBack)
        
        *-- Liege ich jetzt vor dem RefDate, dann
        *-- ein Jahr weiter, denn ich will den nächsten
        *-- Geburtstag haben
        if ldRetVal < tdRefDate
            ldRetVal = This.DateToRef( tdBirthDate, goMonth(tdRefDate, 12), tlBack)
        endif
        

*--         lnDay     = day(ldRetVal)
*--         lnMonth = month(ldRetVal)
*--         
*--         *-- Am 29. eines Schaltjahres gebohren?
*--         if lnDay = 29 and lnMonth = 2
*--             ldX = date(year(tdRefDate),3,1)
*-- 
*--             if ldX < tdRefDate
*--                 ldX = goMonth(ldX,12)
*--             endif
*--             
*--             if day(ldX-1) = 28
*--                 *-- Kein SchaltJahr
*--                 ldRetval = iif(tlBack, ldX-1,ldX)
*--             else
*--                 ldRetVal = ldX-1
*--             endif        
*--         
*--         else    
*--             ldRetVal = date(year(tdRefDate), month(ldRetVal), day(ldRetVal))
*--             
*--             if ldRetVal < tdRefDate
*--                 *-- Dies' Jahr schon vorbei, dann
*--                 *-- nächstes
*--                 ldRetVal = goMonth(ldRetVal, 12)
*--             endif
*-- 
*--         endif

    endif

    This.dBirthDay = ldRetVal

    return ldRetVal
    *-- eop NextBirthday
    
    
    *=========================================================
    *
    *         PROCEDURE: Age             
    *
    *=========================================================
    PROCEDURE Age             
    *  Created...........:  1.August 2003, 12:07 Uhr
    *  Changed...........:   
    *  Description.......:  Return the Age at a certain Date
    *  Calling Samples...:  ? Age
    *  Parameters........:  tdBirthDate, tdRefDate
    *  Returns...........:  numeric (age)
    lparameters    tdBirthDate, tdRefDate
    LOCAL lnRetVal
    tdBirthDate = iif(vartype(tdBirthDate) = "D", tdBirthDate, This.dBirthDate)
    tdRefDate     = iif(vartype(tdRefDate) = "D", tdRefDate, This.dBirthDay)
    
    *===================================================================*
    *  Als Default für tdRefDate nehme ich dBirthday. Damit ist ohne    *
    *  weitere Übergabe eine Abfrage möglich wie:                       *
    *  Wann ist der nächste Geburtstag und wie alt werde ich dann sein  *
    *  ? oBirthday.NextBirthday(<d1>, <d2>)                             *
    *  ? oBirthday.Age()                                                *
    *===================================================================*
    
    *-- Ein Jahr hat genau  365.242199 Tage. Es gibt, wenn ich mit
    *-- Tagen rechne aber um den Geburtstag herum immer Probleme, daher
    *-- hantiere ich einfach mit Jahren
    
    lnRetVal = Year(tdRefDate)-Year(tdBirthDate)
    
    *-- Ist der auf das Jahr des RefDate gebrachte gebrachte
    *-- Geburtstag <= tdRefDate, dann hat der Geburtstag bereits
    *-- stattgefunden, andernfalls steht er noch aus.
    if This.DateToRef(tdBirthDate, tdRefDate) > tdRefDate 
        lnRetVal = lnRetVal - 1
    endif
    return lnRetVal
    *-- eop Age


    *=========================================================
    *
    *         PROCEDURE: DateToRef             
    *
    *=========================================================
    PROCEDURE DateToRef             
    *  Created...........:  1.August 2003, 14:46 Uhr
    *  Changed...........:   
    *  Description.......:  Bring a certain Date to a current Year
    *  Calling Samples...:  tdBirhDate, tdRefDate
    *  Parameters........:  tdBirthDate, tdRefDate
    *  Returns...........:  Date
    lparameters    tdBirthDate, tdRefDate, tlBack
    tdBirthDate = iif(vartype(tdBirthDate) = "D", tdBirthDate, This.dBirthDate)
    tdRefDate   = iif(vartype(tdRefDate) = "D", tdRefDate, date())
    tlBack      = iif(parameters()=3, tlBack, This.lDateBack)

    LOCAL ldX, lnDay, lnMonth, ldRetval, lnYear
    
    ldRetval = tdBirthDate

    if ! empty(ldRetVal)
        lnDay     = day(ldRetVal)
        lnMonth = month(ldRetVal)
        lnYear  = year(tdRefDate)
        
        *-- Am 29. eines Schaltjahres gebohren?
        if lnDay = 29 and lnMonth = 2
        
            *-- Als Dummdy-Date den 1. März
            ldX = date(lnYear, 3, 1)
            
            if day(ldX-1) = 28
                *-- Kein SchaltJahr, dann je nach Konfiguration
                *-- den ersten März oder den 28 Februar wählen
                ldRetval = iif(tlBack, ldX-1, ldX)
            else
                *-- Andernfalls den 29.
                ldRetVal = ldX-1
            endif        
        
        else
            *-- Normales Datum    
            ldRetVal = date(lnYear, lnMonth, lnDay)
            
        endif

    endif    

    return ldRetVal
    *-- eop DateToRef

                            
enddefine
HTH

Frank
Regards from Berlin

Frank

Dietrich Datentechnik (Berlin)
Softwarekombinat Teltow (Teltow)

Frank.Dietrich@dd-tech.de
DFPUG # 327
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform