Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Coding puzzle 2
Message
De
04/06/2002 16:01:12
 
 
À
04/06/2002 12:30:50
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Titre:
Divers
Thread ID:
00664570
Message ID:
00664703
Vues:
19
>Here's a nice contest. Who can write the shortest function (using native vfp functions/commands) that returns a person's age. Input is the birthday and the current date. The winner will get 3 points from me!

I'm not sure if this is the shortest way of doing that, but think it will return all information you may want <s>

Fernando
 nTotalDays = BetweenDates (dBirthDate, Date (), @nYears @nMonths, @nDays)

 Function BetweenDates 
*-------- ------------
 LParameters pStartDate, pEndDate, pYears, pMonths, pDays
 Local       dStartDate, dEndDate, dPreCmpDate

 dPreCmpDate = {}

 dStartDate  = Min (pStartDate, pEndDate) && Just in case you forget parameters order :)
 dEndDate    = Max (pStartDate, pEndDate)

 pYears      = 0
 pMonths     = 0
 pDays       = 0

 dEndOfMonth = CtoD (AllTrim (Str (Month (dEndDate)))  + '/' + "01" + '/' + ;
                     AllTrim (Str (Year  (dEndDate)))) - 1

 If Month (dStartDate) <= Month (dEndDate)

    pYears = Year (dEndDate) - Year (dStartDate)

    If Day (dStartDate) <= Day (dEndDate)
       pMonths = Month (dEndDate) - Month (dStartDate)
       pDays   = Day   (dEndDate) - Day   (dStartDate)
    else
       If Month (dStartDate) = Month (dEndDate)
          pYears = pYears - 1
       endif
       pMonths = Mod (Month (dEndDate) - Month (dStartDate) - 1 + 12, 12)
       pDays = dEndOfMonth - dPreCmpDate + Day (dEndDate)
   endif

 Else

   pYears = Year (dEndDate) - Year (dStartDate) - 1

   If Day (dStartDate) > Day (dEndDate)
      pMonths = Month (dEndDate) - Month (dStartDate) + 12 - 1
      pDays   = dEndOfMonth - dPreCmpDate + Day (dEndDate)
   else
      pMonths = Month (dEndDate) - Month (dStartDate) + 12
      pDays   = Day   (dEndDate) - Day   (dStartDate)
   endif

 EndIf

 Return (pEndDate - pStartDate)

 EndFunc                        &&  of BetweenDates
PS. I found this routine in a MSDN KB article, if my memory doesn't fail.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform