With this method you get will get wrong results( for example age =
11/20/1970 ). For date calculations, including ageInYears we use a DateUtils
class (see below).

Harald



package de.financetec.utils
{
        import de.financetec.core.Error2;
        
        /**
         * DateUtils ist eine statische Klasse die Funktionen zum Vergleich
und zur Manipulation 
         * von Date Variablen zur Verfügng stellt.
         * 
         * @author Harald Dehn
         * 
         */
        public class DateUtils
        {
                /**
                * Wochentag Montag.
                */
                public static const MONDAY:int
= 1;
                /**
                * Wochentag Dienstag.
                */
                public static const TUESDAY:int
= 2;
                /**
                * Wochentag Mittwoch.
                */
                public static const WEDNESDAY:int
= 3;
                /**
                * Wochentag Donnerstag.
                */
                public static const THURSDAY:int
= 4;
                /**
                * Wochentag Freitag.
                */
                public static const FRIDAY:int
= 5;
                /**
                * Wochentag Samstag.
                */
                public static const SATURDAY:int
= 6;
                /**
                * Wochentag Sonntag.
                */
                public static const SUNDAY:int
= 0;
        
                /**
                * Zeitteil Sekunde.
                */
                public static const DATEPART_SECOND:String
= "s";
                /**
                * Zeitteil Minute.
                */
                public static const DATEPART_MINUTE:String
= "n";
                /**
                * Zeitteil Stunde.
                */
                public static const DATEPART_HOUR:String
= "h";
                /**
                * Datumsteil Tag.
                */
                public static const DATEPART_DAY:String
= "d";
                /**
                * Tag in der Woche.
                */
                public static const DATEPART_DAY_OF_WEEK:String
= "dw";  
                /**
                * Tag im Jahr (Anzahl der Tage seit Jahresbeginn).
                */      
                public static const DATEPART_DAY_OF_YEAR:String
= "dy";  
                /**
                * Kalenderwoche im Jahr.
                */
                public static const DATEPART_WEEK:String
= "w";
                /**
                * Datumsteil Monat.
                */
                public static const DATEPART_MONTH:String
= "m";
                /**
                * Datumsteil Quartal (Jan-Mär = 1 und Okt-Dez = 4,
entspricht 3 Monate bei dateAdd()).
                */
                public static const DATEPART_QUARTAL:String
= "q";
                /**
                * Datumsteil Jahr.
                */
                public static const DATEPART_YEAR:String
= "y";


                private static const MILLISECONDS_PER_SECOND:Number     =
1000;
                private static const MILLISECONDS_PER_MINUTE:Number     =
1000 * 60;
                private static const MILLISECONDS_PER_HOUR:Number
= 1000 * 60 * 60;
                private static const MILLISECONDS_PER_DAY:Number
= 1000 * 60 * 60 * 24;
                private static const MILLISECONDS_PER_WEEK:Number
= 1000 * 60 * 60 * 24 * 7;

                // Public Static
--------------------------------------------------------------------

                /**
                 * Alter einer Person in Monaten.
                 * 
                 * @param pBirthdate Geburtdatum der Person.
                 * @return Alter der Person in Monaten.
                 * 
                 */
                public static function ageInMonth(pBirthdate:Date):Number 
                {
                        var age:Number = getUTCMonthDiff(pBirthdate, new
Date());
                        return (age >= 0) ? age : 0;
                }

                /**
                 * Alter einer Person in Jahren.
                 * 
                 * @param pBirthdate Geburtsdatum der Person.
                 * @return Alter der Person in Jahren.
                 * 
                 */
                public static function ageInYears(pBirthdate:Date):Number 
                {
                        return ageInMonth(pBirthdate) / 12;
                }

                /**
                 * Teile eines Datums (z.B. Sekunden, Tage, Wochen,
Quartale, ...) addieren bzw. subtrahieren.
                 * 
                 * <p>
                 * @param pDatePart Teil des Datums das verändert werden
soll (erlaubte Werte <b>see also</b>).
                 * @param pValue Anzahl der Teile die addiert, bzw bei
negativen Werten subtrahiert werden.
                 * @param pDate Anfangsdatum.
                 * @return verändertes Datum.
                 * 
                 * @see #DATEPART_SECOND
                 * @see #DATEPART_MINUTE
                 * @see #DATEPART_HOUR
                 * @see #DATEPART_DAY
                 * @see #DATEPART_WEEK
                 * @see #DATEPART_MONTH
                 * @see #DATEPART_QUARTAL
                 * @see #DATEPART_YEAR
                 * 
                 * @example
                 *  <listing version="3.0">
                 *   var datum:Date = new Date(2007, 2, 10);            //
10.03.2007
                 *   trace(dateAdd(DateUtils.partMonth, 11, datum));    //
10.02.2008
                 * </listing>
                 */
                public static function dateAdd(pDatePart:String, pValue:int,
pDate:Date):Date
                {
                        var resultDate:Date = new Date(pDate.getTime());
                        switch( pDatePart.toLowerCase() )
                        {
                                case DATEPART_SECOND:
                                        resultDate.secondsUTC += pValue;
                                        break;
                                case DATEPART_MINUTE:
                                        resultDate.minutesUTC += pValue;
                                        break;
                                case DATEPART_HOUR:
                                        resultDate.hoursUTC += pValue;
                                        break;
                                case DATEPART_DAY:
                                        resultDate.dateUTC += pValue;
                                        break;
                                case DATEPART_WEEK:
                                        resultDate.dateUTC += (7 * pValue);
                                        break;
                                case DATEPART_MONTH:
                                        resultDate.monthUTC += pValue;
                                        break;
                                case DATEPART_QUARTAL:
                                        resultDate.monthUTC += (3 * pValue);
                                        break;
                                case DATEPART_YEAR:
                                        resultDate.fullYearUTC += pValue;
                                        break;
                                default:
                                        throw Error2.create("Datepart: {s}
not valid for dateAdd.", pDatePart);
                        }
                        return resultDate;
                }

                /**
                 * Differenz zwischen zwei Datumswerten in (z.B. Sekunden,
Tage, Wochen, Quartale, ...).
                 * 
                 * <p>
                 * @param pDatePart Einheit in der die Differenz berechnet
wird (erlaubte Werte <b>see also</b>).
                 * @param pDate1 Anfangsdatum für den Vergleich.
                 * @param pDate2 Enddatum für den Vergleich.
                 * @return Differenz zwischen End- und Anfangsdatum in der
angegebenen Einheit.
                 * 
                 * @see #DATEPART_SECOND
                 * @see #DATEPART_MINUTE
                 * @see #DATEPART_HOUR
                 * @see #DATEPART_DAY
                 * @see #DATEPART_WEEK
                 * @see #DATEPART_MONTH
                 * @see #DATEPART_QUARTAL
                 * @see #DATEPART_YEAR
                 */
                 public static function dateDiff(pDatePart:String,
pDate1:Date, pDate2:Date):Number
                 {
                        var milliseconds:Number = pDate2.time - pDate1.time
+ 
        
(pDate1.timezoneOffset - pDate2.timezoneOffset) * MILLISECONDS_PER_MINUTE;

                        switch( pDatePart.toLowerCase() )
                        {
                                case DATEPART_SECOND:
                                        return int(milliseconds /
MILLISECONDS_PER_SECOND);
                                case DATEPART_MINUTE:
                                        return int(milliseconds /
MILLISECONDS_PER_MINUTE);
                                case DATEPART_HOUR:
                                        return int(milliseconds /
MILLISECONDS_PER_HOUR);
                                case DATEPART_DAY:
                                        return int(milliseconds /
MILLISECONDS_PER_DAY);
                                case DATEPART_WEEK:
                                        return int(milliseconds /
MILLISECONDS_PER_WEEK);
                                case DATEPART_MONTH:
                                        return getUTCMonthDiff(pDate1,
pDate2);
                                case DATEPART_QUARTAL:
                                        return int(getUTCMonthDiff(pDate1,
pDate2) / 3);
                                case DATEPART_YEAR:
                                        return int(getUTCMonthDiff(pDate1,
pDate2) / 12);
                                default:
                                        throw Error2.create("Datepart: {s}
not valid for dateDiff.", pDatePart);
                        }
                }

                /**
                 * Gibt eines Teil eines Datums zurück, z.B. Woche im Jahr.
                 * 
                 * @param pDatePart Teil des Datums der ausgegeben wird
(erlaubte Werte <b>see also</b>).
                 * @param pDate Datum.
                 * @param pFirstDayOfWeek Erster Tag der Woche für die
Wochenberechnung (default = dayMonday).
                 * @return Teil des Datums als numerischer Wert.
         *
                 * @see #DATEPART_SECOND
                 * @see #DATEPART_MINUTE
                 * @see #DATEPART_HOUR
                 * @see #DATEPART_DAY
                 * @see #DATEPART_DAY_OF_WEEK
                 * @see #DATEPART_DAY_OF_YEAR
                 * @see #DATEPART_WEEK
                 * @see #DATEPART_MONTH
                 * @see #DATEPART_QUARTAL
                 * @see #DATEPART_YEAR
                 * 
                 */
                public static function datePart(pDatePart:String,
pDate:Date, pFirstDayOfWeek:int = MONDAY):Number
                {
                        switch( pDatePart.toLowerCase() )
                        {
                                case DATEPART_SECOND:
                                        return pDate.seconds;
                                case DATEPART_MINUTE:
                                        return pDate.minutes;
                                case DATEPART_HOUR:
                                        return pDate.hours;
                                case DATEPART_DAY:
                                        return pDate.date;
                                case DATEPART_DAY_OF_WEEK:
                                        return pDate.day;
                                case DATEPART_DAY_OF_YEAR:
                                        return dateDiff(DATEPART_DAY, new
Date(pDate.fullYear, 1, 1), pDate) + 1;
                                case DATEPART_MONTH:
                                        return pDate.month + 1;
                                case DATEPART_WEEK:
                                        // Starts at 01.01. TODO andere
Berechnungen
                                        var firstOfYear:Date =
getFirstDateOfYear(pDate);
                                        var delta:int = firstOfYear.day -
pFirstDayOfWeek;
                                        if( delta < 0 ) delta += 7;
                                        var week:int =
int((dateDiff(DATEPART_DAY, firstOfYear, pDate) + delta) / 7) + 1;
                                        if( week <= 0 ) 
                                                week += 52;
                                        else if( week >= 52 ) {
                                                firstOfYear.fullYear += 1;
                                                delta = firstOfYear.day -
pFirstDayOfWeek;
                                                if( delta < 0 ) delta += 7;
                                                if( dateDiff(DATEPART_DAY,
getFirstDateOfDay(pDate), firstOfYear) <= delta )
                                                        week = 1;
                                        } 
                                        return week;
                                case DATEPART_QUARTAL:
                                        return int(pDate.month / 3) + 1;
                                case DATEPART_YEAR:
                                        return pDate.fullYear;
                                default:
                                        throw Error2.create("Datepart: {s}
not valid.", pDatePart);
                        }
                }
                
                /**
                 * Gibt die Anzahl der Tage vom heutigen Tag bis zum
Geburtstag einer Person für das aktuelle Jahr an.
                 * Befindet sich der Geburtstag in der Zukunft, wird die
Anzahl der Tage als positiver
                 * Wert zurückgegeben, andernfalls als negativer Wert.
                 * 
                 * @param pBirthDate    Geburtsdatum der Person.
                 * @return                              Tage bis zum
Geburtstag (negativer Wert, wenn Geburtstag in der Vergangenheit liegt).
                 * 
                 */
                public static function
daysToBirthday(pBirthDate:Date):Number
                {
                        if( !pBirthDate ) 
                                return NaN;
                        
                        var compareDate:Date = new Date();
                        compareDate.fullYear = pBirthDate.fullYear;
                        
                        var days:Number = dateDiff(DATEPART_DAY,
compareDate, pBirthDate);
                        
                        if( days > 182 ) 
                        {
                                compareDate.fullYear++;
                                return dateDiff(DATEPART_DAY, compareDate,
pBirthDate);
                        }
                        else if( days < -182 ) 
                        {
                                compareDate.fullYear--;
                                return dateDiff(DATEPART_DAY, compareDate,
pBirthDate);
                        }
                        else
                                return days;
                }
                
                /**
                 * Gibt das Datum mit der Uhrzeit 00:00:00 einer Date
Variablen zurück.
                 * 
                 * @param pDate Datum.
                 * 
                 */
                public static function getFirstDateOfDay(pDate:Date):Date
                {
                        return new Date(pDate.fullYear, pDate.month,
pDate.date);
                }
                /**
                 * Gibt erste Datum der Woche mit der Uhrzeit 00:00:00 einer
Date Variablen zurück.
                 * 
                 * @param pDate Datum.
                 * @param firstDayOfWeek Erster Tag der Woche für die
Wochenberechnung (default = dayMonday).
                 * 
                 */
                public static function getFirstDateOfWeek(pDate:Date,
firstDayOfWeek:int = MONDAY):Date
                {
                        var delta:int = pDate.day - firstDayOfWeek;
                        if( delta < 0 ) delta += 7;
                        return new Date(pDate.fullYear, pDate.month,
pDate.date - delta);
                }
                /**
                 * Gibt erste Datum des Monats mit der Uhrzeit 00:00:00
einer Date Variablen zurück.
                 * 
                 * @param pDate Datum.
                 * 
                 * @example
                 *  <listing version="3.0">
                 *   var datum:Date = new Date(2007, 2, 10);    //
10.03.2007
                 *   trace(getFirstDateOfMonth(datum));         //
01.03.2007
                 * </listing>
                 */
                public static function getFirstDateOfMonth(pDate:Date):Date
                {
                        return new Date(pDate.fullYear, pDate.month, 1);
                }
                /**
                 * Gibt erste Datum des Quartals mit der Uhrzeit 00:00:00
einer Date Variablen zurück.
                 * @param pDate Datum.
                 * 
                 * @example
                 *  <listing version="3.0">
                 *   var datum:Date = new Date(2007, 4, 10);    //
10.05.2007
                 *   trace(getFirstDateOfQuartal(datum));       //
01.04.2007
                 * </listing>
                 */
                public static function
getFirstDateOfQuartal(pDate:Date):Date
                {
                        return new Date(pDate.fullYear, int(pDate.month / 3)
* 3, 1);
                }
                /**
                 * Gibt erste Datum des Jahres mit der Uhrzeit 00:00:00
einer Date Variablen zurück.
                 * @param pDate Datum.
                 * 
                 * @example
                 *  <listing version="3.0">
                 *   var datum:Date = new Date(2007, 4, 10);    //
10.05.2007
                 *   trace(getFirstDateOfYear(datum));          //
01.01.2007
                 * </listing>
                 */
                public static function getFirstDateOfYear(pDate:Date):Date
                {
                        return new Date(pDate.fullYear, 0, 1);
                }
                /**
                 * Gibt das Datum mit der Uhrzeit 23:59:59 einer Date
Variablen zurück.
                 * 
                 * @param pDate Datum.
                 * 
                 */
                public static function getLastDateOfDay(pDate:Date):Date {
                        return new Date(pDate.fullYear, pDate.month,
pDate.date + 1, 0, 0, 0, -1);
                }
                /**
                 * Gibt letzte Datum der Woche mit der Uhrzeit 23:59:59
einer Date Variablen zurück.
                 * 
                 * @param pDate Datum.
                 * @param pFirstDayOfWeek Erster Tag der Woche für die
Wochenberechnung (default = dayMonday).
                 * 
                 */
                public static function getLastDateOfWeek(pDate:Date,
pFirstDayOfWeek:int = MONDAY):Date {
                        var delta:int = pDate.day - pFirstDayOfWeek;
                        if( delta < 0 ) delta += 7;
                        return new Date(pDate.fullYear, pDate.month,
pDate.date - delta + 7, 0, 0, 0, -1);
                }
                /**
                 * Gibt letzte Datum des Monats mit der Uhrzeit 23:59:59
einer Date Variablen zurück.
                 * 
                 * @param pDate Datum.
                 * 
                 * @example
                 *  <listing version="3.0">
                 *   var datum:Date = new Date(2007, 2, 10);    //
10.03.2007 00:00:00
                 *   trace(getLastDateOfMonth(datum));         // 31.03.2007
23:59:59
                 * </listing>
                 */
                public static function getLastDateOfMonth(pDate:Date):Date
                {
                        return new Date(pDate.fullYear, pDate.month + 1, 1,
0, 0, 0, -1);
                }
                /**
                 * Gibt letzte Datum des Quartals mit der Uhrzeit 23:59:59
einer Date Variablen zurück.
                 * @param pDate Datum.
                 * 
                 * @example
                 *  <listing version="3.0">
                 *   var datum:Date = new Date(2007, 4, 10);    //
10.05.2007 00:00:00
                 *   trace(getLastDateOfQuartal(datum));       // 30.06.2007
23:59:59
                 * </listing>
                 */
                public static function getLastDateOfQuartal(pDate:Date):Date
                {
                        return new Date(pDate.fullYear, (int(pDate.month /
3) + 1) * 3, 1, 0, 0, 0, -1);
                }
                /**
                 * Gibt das letzte Datum des Jahres mit der Uhrzeit 23:59:59
einer Date Variablen zurück.
                 * @param pDate Datum.
                 * 
                 * @example
                 *  <listing version="3.0">
                 *   var datum:Date = new Date(2007, 4, 10);    //
10.05.2007 00:00:00
                 *   trace(getLastDateOfYear(datum));          // 31.12.2007
23:59:59
                 * </listing>
                 */
                public static function getLastDateOfYear( pDate:Date ):Date
                {
                        return new Date(pDate.fullYear + 1, 0, 1, 0, 0, 0,
-1);
                }
                
                /**
                 * Gibt das übergebene Datum als Date-Objekt zurück.
                 * @param pDate                 Datum im Format DD.MM.YYYY
                 * @return                              Datum
                 * 
                 */             
                public static function getDateFromString( pDate:String
):Date
                {
                        var dateSplit:Array = pDate.split( "." );
                        
                        var date:Date = new Date( dateSplit[ 2 ], dateSplit[
1 ] * 1 - 1, dateSplit[ 0 ] );
                        return date;
                }
                                
                // Private static
--------------------------------------------------------------------

                private static function getUTCMonthDiff(pDate1:Date,
pDate2:Date):Number
                {
                        var month:Number        = (pDate2.getUTCFullYear() -
pDate1.getUTCFullYear()) * 12 + 
        
pDate2.getUTCMonth() - pDate1.getUTCMonth();
                        var msDate1:Number      = pDate1.getUTCDate() *
MILLISECONDS_PER_DAY + 
        
getUTCMillisecondsSinceMidnight(pDate1);
                        var msDate2:Number      = pDate2.getUTCDate() *
MILLISECONDS_PER_DAY + 
        
getUTCMillisecondsSinceMidnight(pDate2);

                        if( month > 0 && msDate1 > msDate2 ) 
                                month--;
                        else if( month < 0 && msDate1 < msDate2 ) 
                                month++;
                        return month;
                }
                
                private static function
getUTCMillisecondsSinceMidnight(pDate:Date):Number
                {
                        return ((pDate.getUTCHours() * 60 
                                                + pDate.getUTCMinutes()) *
60 
                                                + pDate.getUTCSeconds()) *
1000 
                                                +
pDate.getUTCMilliseconds();
                }
        }
}

-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Tom Chiverton
Sent: Mittwoch, 3. Oktober 2007 10:45
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Return a person's age

On Tuesday 02 Oct 2007, [EMAIL PROTECTED] wrote:
> var age = new Date(birthDate.getTime() - todayDate.getTime);

That gets you a Date representing the difference.

ageInYears:Number=new Date().getFullYear() - age.getFullYear()

-- 
Tom Chiverton
Helping to globally extend prospective interfaces
on: http://thefalken.livejournal.com

****************************************************

This email is sent for and on behalf of Halliwells LLP.

Halliwells LLP is a limited liability partnership registered in England and
Wales under registered number OC307980 whose registered office address is at
St James's Court Brown Street Manchester M2 2JF.  A list of members is
available for inspection at the registered office.  Any reference to a
partner in relation to Halliwells LLP means a member of Halliwells LLP.
Regulated by The Solicitors Regulation Authority.

CONFIDENTIALITY

This email is intended only for the use of the addressee named above and may
be confidential or legally privileged.  If you are not the addressee you
must not read it and must not use any information contained in nor copy it
nor inform any person other than Halliwells LLP or the addressee of its
existence or contents.  If you have received this email in error please
delete it and notify Halliwells LLP IT Department on 0870 365 8008.

For more information about Halliwells LLP visit www.halliwells.com.


--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links




Reply via email to