Editor dependent fields to calculate age

Editor dependent fields to calculate age

lincolncbennettlincolncbennett Posts: 25Questions: 11Answers: 1

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

  • lincolncbennettlincolncbennett Posts: 25Questions: 11Answers: 1

    Okay I have think I have solved this for calculating seconds between 2 dates.

    //editor dependent
          editor1.dependent( [ 'importtest.DeceasedDateOfBirth', 'importtest.DeceasedDateOfDeathExact' ], function ( val ) {
          if(editor1.field('importtest.DeceasedDateOfBirth').val() !== '' && editor1.field('importtest.DeceasedDateOfDeathExact').val() !== '') 
          {     //date of birth seconds
          var dob = editor1.field('importtest.DeceasedDateOfBirth').val();
          var dobms = new Date(dob);
          var dobs = dobms.getTime()/1000;
         //date of death seconds
          var dod = editor1.field('importtest.DeceasedDateOfDeathExact').val();
          var dodms = new Date(dod);
          var dods = dodms.getTime()/1000;
          //age in seconds calculation
          var age = (dods - dobs);
          editor1.field('importtest.DeceasedAge').val(age);
             }
          } );
    

    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.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Glad you're sorted, shout if you need any assistance.

    Colin

This discussion has been closed.