Best Way to Calculate Age - DHTMLXGRID

I am using the DHTMLX GRID, and am importing a CSV that, among other things, contains birthdates. I would like to populate a column with their current age, based on the current date. I have tried mayn different things, but just can’t figure out how to do this.



Any help would be appreciated!

You can use code similar to next ( code suppouse that data stored in default mm/dd/yyyy format )

grid.attachEvent(“onRowCreated”,function(id){
var date = grid.cells(id,INDEX).getValue(); //get date
var years = (new Date()).getYear()-(Date.parse(date)).getYear();
grid.cells(id,INDEX).setValue(years); //set years
return true;
});

INDEX - index of column in question

I have tries this, and can’t get it to work.  I get Object does not exist errors.

Please check attached sample.
1224061519.zip (92.5 KB)

Thanks!  Now I see what I was doing wrong.  I have to say… I am VERY IMPRESSED with the speed and quality of support!

I have dates in format - dd/mm/yyyy and need convert it before calculating age, how correctly write an array converting?

mygrid.attachEvent("onRowCreated",function(id){ var date = mygrid.cells(id,INDEX).getValue(); //get dd/mm/yyyy date var datearray = date.split("/"); var newdate = datearray[1] + '/' + datearray[0] + '/' + datearray[2]; //<-- ????????? var years = (new Date()).getYear()-(new Date(newdate)).getYear(); mygrid.cells(id,INDEX).setValue(years); //set years return true; });

You can place parameters directly in Date constructor

[code]var month = datearray[0];
var day = datearray[1];
var year = datearray[2];

var years = (new Date()).getYear()-(new Date(year, month, day)).getYear();[/code]

Stanislav, now I get NaN in each sell:

[code]var INDEX = 2;
mygrid.attachEvent(“onRowCreated”,function(id){
var date = mygrid.cells(id,INDEX).getValue(); //get dd/mm/yyyy date
var datearray = date.split("/");
var month = datearray[0];
var day = datearray[1];
var year = datearray[2];
//var newdate = datearray[1] + ‘/’ + datearray[0] + ‘/’ + datearray[2];

	   var years = (new Date()).getYear()-(new Date(year, month, day)).getYear();
       mygrid.cells(id,INDEX).setValue(years); //set years
       return true;
});  [/code]

maybe datearray is not array?

if you are using d/m/y format - it must have different indexes for datearray
Try to change code as

var month = datearray[1]; var day = datearray[0]; var year = datearray[2];

Nope. Let’s look again

[code]…
mygrid.init();
var INDEX = 2;
mygrid.attachEvent(“onRowCreated”,function(id){
var date = mygrid.cells(id,INDEX).getValue(); //now date becomes an array with dd/mm/yyyy date values (?)
var datearray = date.split(“/”); // ← what’s happen with date after this?
var month = datearray[1]; // ← is month an array of monthes from grid date fields?
var day = datearray[0];
var year = datearray[2];

	   var years = (new Date()).getYear()-(new Date(1986, 05, 07)).getYear(); //<-- with constant values it works
       mygrid.cells(id,INDEX).setValue(years); //set years
       return true;
});

mygrid.loadXML(“./grid_data”);
…[/code]

But when I change Date(1986, 05, 07) to Date(year, month, day) it wouldn’t work

I SOLVED problem! the code was nearly true, but I try to setValue to column with type dhxCalendarA!!! When i try to set it in another column (type ‘ed’), it becomes great!
Notice: deduct ‘-1’ from month, because javascript count monthes from ‘0’.

Here is working code:

var INDEX = 1; var month = new Array(); mygrid.attachEvent("onRowCreated",function(id){ date = mygrid.cells(id,INDEX).getValue(); //get dd/mm/yyyy date var datearray = date.split("/"); var month = datearray[1]; var day = datearray[0]; var year = datearray[2]; var years = (new Date()).getYear()-(new Date(year, month-1, day)).getYear(); mygrid.cells(id,2).setValue(years); //set years return true; });

Please check the attached sample.
grid_date_age.zip (120 KB)