Results 1 to 9 of 9

Thread: calculate age automatically given the birthdate

  1. #1
    Ext User
    Join Date
    Mar 2009
    Posts
    54

    Default calculate age automatically given the birthdate

    hi,

    i have a form that a user fills up with his personal data. when he enters the birthdate i want to calc the age automatically and show it in the age field.
    my idea was to add a listener to birthdate field and then calc the age.

    the problem i have is how to extract the value of the date field each one separetely (year-month-day) so then i can calculate the differences btw the 2 years, 2 months, 2 days

    with the Ext.getCmp('bdate').getValue() method it shows in this way Thu Sep 08 2005 00:00:00 GMT+0200 (ora solare Europa occidentale)

    Code:
    {
                                    xtype:'datefield',
                                    fieldLabel: 'Data di nascita',
                                    name: 'data_nascita',
                                    id:'bdate',
                                    format:'Y-m-d',
                                    allowBlank:false,
                                    blankText:'Campo obbligatorio',        
                                    width:220
                                    ,listeners:{select: function() {
                                        var calcAge = Ext.getCmp('age-id');  
                                        var currentTime = new Date();
                                        var month = currentTime.getMonth() + 1;
                                        var day = currentTime.getDate();                                    
                                        var year = currentTime.getFullYear();
                                        .....
                                        .....
                                        .....
    
                                       var age = ....
                                            calAge.setValue(age);
                                        }}
                                },{
                                    xtype:'textfield',
                                    fieldLabel: 'Et',
                                    name: 'eta',
                                    id:'age-id',
                                    emptyText: '0',
                                    width:220
                                }
    i also tried to parse the date but no succes
    thnx for any help

  2. #2
    Sencha User dlbjr's Avatar
    Join Date
    Oct 2007
    Location
    Cache, OK USA
    Posts
    281
    dlbjr - David L. Bryant Jr.

    Owner of
    dlbjr Technology Consulting
    Web2 System Developer & Consultant
    Focused on C.I. - Six Sigma - Shingo - Lean Technologies
    [email protected]

    Owner of:
    Attractive Graphics - "custom screen printing"
    [email protected]

  3. #3
    Ext User
    Join Date
    Mar 2009
    Posts
    54

    Default

    but the problem is how to parse the date from the datefield not how to calculate the age. i mean i want to parse the date in the format year; month; day

    ex. if i enter the value 2005-09-09 then when retriving the value from datefield it is shown as Thu Sep 08 2005 00:00:00 GMT+0200 (ora solare Europa occidentale).

  4. #4
    Sencha User dlbjr's Avatar
    Join Date
    Oct 2007
    Location
    Cache, OK USA
    Posts
    281

    Default Sample Idea

    Code:
     
    //Place this in your global area
    Date.patterns = {
         ISO8601Long: 'Y-m-d H:i:s',
         ISO8601Short: 'Y-m-d',
         ShortDate: 'n/j/Y',
         LongDate: 'l, F d, Y',
         FullDateTime: 'l, F d, Y g:i:s A',
         MonthDay: 'F d',
         ShortTime: 'g:i A',
         LongTime: 'g:i:s A',
         SortableDateTime: 'Y-m-d\\TH:i:s',
         YearMonth: 'F, Y',
         DatabaseLong: 'YmdHis',
         DatabaseShort: 'Ymd',
         DatabaseTime: 'His'
    };
     
    //Sample Code
    var parseddate = Date.parseDate(Ext.get('bdate').getValue(), Date.patterns.DatabaseShort);
    dlbjr - David L. Bryant Jr.

    Owner of
    dlbjr Technology Consulting
    Web2 System Developer & Consultant
    Focused on C.I. - Six Sigma - Shingo - Lean Technologies
    [email protected]

    Owner of:
    Attractive Graphics - "custom screen printing"
    [email protected]

  5. #5
    Ext User
    Join Date
    Mar 2009
    Posts
    54

    Default

    thnx dlbjr,
    in my case i didn't need all the Date.pattern so i just wrote
    Code:
    var parseddate = Date.parseDate(Ext.get('bdate').getValue(), 'Y-m-d');
    and then extracted each value.
    Code:
     var month = parseddate.format('m')-1;
    var date = parseddate.format('d');
    var year = parseddate.format('Y');
    (wrote code in case somebody has the same issue.)
    i got the age calculation working now

  6. #6
    smedleyt's Avatar
    Join Date
    Jun 2008
    Location
    Clermont, Florida
    Posts
    1

    Default lil help

    Elona mind posting the code for the age calc I can see this being used often in the very near future.

  7. #7
    Ext User
    Join Date
    Mar 2009
    Posts
    54

    Default

    Quote Originally Posted by smedleyt View Post
    Elona mind posting the code for the age calc I can see this being used often in the very near future.
    hi smedleyt
    it's a long time i did it so i don't remember very well how it all works

    i had a field in a form where you entered the birth date and the listener automatically updates the field of the age:
    Code:
                         {
                                    xtype:'datefield',
                                    fieldLabel: 'birthdate',
                                    name: 'birthdate',
                                    id:'bdate',
                                    format:'Y-m-d',
                                    allowBlank:false,
                                    blankText:'Campo obbligatorio',        
                                    width:220
                                    ,listeners:{select: function() {
                                        var age = Ext.getCmp('age-id');  
                                        var currentTime = new Date();
                                        var cmonth = currentTime.getMonth();
                                        var cdate = currentTime.getDate();                                    
                                        var cyear = currentTime.getFullYear();                                
                                        var parseddate = Date.parseDate(Ext.get('bdate').getValue(), 'Y-m-d');                         
                                        var month = parseddate.format('m')-1;
                                        var date = parseddate.format('d');
                                        var year = parseddate.format('Y');                                    
                                        var theYear = cyear - year;
                                        var theMonth = cmonth - month;
                                        var theDate = cdate - date;
                                        
                                        var days = "";
                                        if (cmonth == 0 || cmonth == 2 || cmonth == 4 || cmonth == 6 || cmonth == 7 || cmonth == 9 || cmonth == 11) days = 31;
                                        if (cmonth == 3 || cmonth == 5 || cmonth == 8 || cmonth == 10) days = 30;
                                        if (cmonth == 1) days = 28;
                                        
                                         if (month < cmonth && date > cdate) { 
                                             theYear = theYear + 1;
                                         }                                                                        
                                         else if (month > cmonth && date <= cdate) { 
                                             theYear = theYear - 1;
                                            theMonth = ((12 - -(theMonth)) + 1);
                                         } 
                                         else if (month > cmonth && date > cdate) { 
                                            theMonth = ((12 - -(theMonth)));
                                        }
                                        if (date < cdate) { 
                                            theDate = theDate; 
                                        }
                                         else if (date == cdate) { 
                                            theDate = 0; 
                                         }
                                        else { 
                                            theYear = theYear - 1;
                                        }                            
                                    age.setValue(theYear);
                                    }}
                                }
    hope it helps you.
    e.

  8. #8

    Default

    "Age in years" is definitely a FAQ-algorithm in any programming language.

    "Date parsing" is not too far behind. ExtJS's designers did a very good job of extending JavaScript's Date type with parseDate method that generally works quite well. (And several other really useful things too.) Because they are extending the JavaScript "prototype" of the Date object-type, it applies to every calculation that you do.

    If your situation is truly screwy you can always resort to "regular expressions" to slice up the input, then construct a new date based on the parts. And you, too, can add this functionality to the prototype Date so that you can use it everywhere.

    But, no matter what you decide to do, you shouldn't have to put any real effort into it, except to type the word "Google." This is something that has been "done to death," so your task is simply to find a snippet of code that does what you want to do.

  9. #9

    Default

    hi,

    Try this...
    Code:
                var a = Ext.getCmp('DateField1').getValue(); //  Thu Sep 08 2005 00:00:00 GMT+0200 
    
                var d = Ext.util.Format.date(a, "m/d/y"); // 08/08/2005
                // See the documentation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •