Editor dependent fields to calculate age
Editor dependent fields to calculate age
Hi, I am trying to calculate age from 2 fields using editor dependent. I want to enter date of birth and date of death in editor and have the age field populated. I am using moment.js and I have no problem doing this in the table once the data is submitted but I want to see it in editor after the date fields are populated (before submitting). I'm using standard date type fields.
Could it be that the format of the date in the field (once populated) is not correct for working out time between dates? At this stage I am only trying to calculate the seconds between the dates.
Any help would be appreciated.
editor1.dependent( [ 'importtest.DeceasedDateOfBirth', 'importtest.DeceasedDateOfDeathExact' ], function ( val, data, callback ) {
if(editor1.field('importtest.DeceasedDateOfBirth').val() != '' &&
editor1.field('importtest.DeceasedDateOfDeathExact').val() != '')
{
var dateofbirth = editor1.field('importtest.DeceasedDateOfBirth').val().getTime()/1000;
var dateofdeathexact = editor1.field('importtest.DeceasedDateOfDeathExact').val().getTime()/1000;
var age = (dateofdeathexact - dateofbirth);
editor1.field('importtest.DeceasedAge').val(age);
}
} );
This question has an accepted answers - jump to answer
Answers
Okay I have think I have solved this for calculating seconds between 2 dates.
Eventually I need a field which measures the age unit eg. days, weeks, months, years and can populate the age field accordingly. I will give it a go and post back my results.
Glad you're sorted, shout if you need any assistance.
Colin