PDA

View Full Version : calculate age automatically given the birthdate



elona
25 Jun 2009, 3:18 AM
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)



{
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

dlbjr
25 Jun 2009, 4:11 AM
"http://www.scripts.com/viewscript/datediff-and-dateadd-functions-now-in-javascript/21453/"

elona
25 Jun 2009, 12:52 PM
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).

dlbjr
25 Jun 2009, 1:04 PM
//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);

elona
25 Jun 2009, 2:57 PM
thnx dlbjr,
in my case i didn't need all the Date.pattern so i just wrote

var parseddate = Date.parseDate(Ext.get('bdate').getValue(), 'Y-m-d'); and then extracted each value.

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 ;)

smedleyt
15 Jan 2010, 8:05 AM
Elona mind posting the code for the age calc I can see this being used often in the very near future.

elona
18 Jan 2010, 1:12 AM
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:

{
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.

Mike Robinson
18 Jan 2010, 7:07 AM
"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." :D 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.

king1231986
22 Jul 2010, 9:33 AM
hi,

Try this...


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