This topic is locked
[SOLVED]

 Calculate how many days have passed since a date on the fly

6/3/2012 8:46:57 AM
PHPRunner General questions
J
joiresende author

The code below calculates the age of a person in years, how would the calculation in days?
var ctrlAge = Runner.getControl(pageid, 'age_field');

var ctrlDate = Runner.getControl(pageid, 'date_of_birth');

now = new Date();

function func() {

var dateofbirth=new Date(ctrlDate.getValue());

var age=now.getFullYear()-dateofbirth.getFullYear();

if (now.getMonth()<dateofbirth.getMonth()) {

age=age-1;

}

ctrlAge.setValue(age);

};
ctrlAge.on('click', func);

J
joiresende author 6/5/2012

Like this:

var ctrlAge = Runner.getControl(pageid, 'age_field');

var ctrlDate = Runner.getControl(pageid, 'date_of_birth');

now = new Date();

function func() {

var dateofbirth=new Date(ctrlDate.getValue());
var age=Math.floor(( Date.parse(now) - Date.parse(dateofbirth) ) / 86400000);if (now.getMonth()<dateofbirth.getMonth()) {

age=age-1;

}

ctrlAge.setValue(age);

};
ctrlAge.on('click', func);