Converting DOB & Entry date (meta) to years | XM Community
Skip to main content

Hi there.  I have both DOB and Entry Date as metadata in my lifecycle surveys.  Simple layout version.  I’ve had java in there to calculate the age and tenure in years, but it doesn’t seem to work in the Simple layout version. I also want to capture the year and month the survey was completed but this is also not working.  Is anyone able to let me know if my code needs to be changed:

In the Look & Feel header I have the following

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script><script>
    moment().format();
</script>

And then in one of my survey questions I have the following JS:

Qualtrics.SurveyEngine.addOnload(function()
{
  var joinDate = moment('${e://Field/Entry%20Date}', 'DD/MM/YYYY');
  var currentDate = moment();

  var Tenure1 = (currentDate.diff(joinDate, 'months')).toString();
  var Unit = ' months'
  var Tenure = Tenure1.concat(Unit)
  
  var birthdate = moment('${e://Field/Date%20of%20birth}', 'DD/MM/YYYY');

  var age1 = (currentDate.diff(birthdate, 'years')).toString();
  var Unit = ' years'
  var age = age1.concat(Unit)

  Qualtrics.SurveyEngine.setEmbeddedData('Tenure', Tenure);
  Qualtrics.SurveyEngine.setEmbeddedData('Timeinservice', Tenure1);

  Qualtrics.SurveyEngine.setEmbeddedData('Age', age);
  Qualtrics.SurveyEngine.setEmbeddedData('Ageinyears', age1);    

});

Qualtrics.SurveyEngine.addOnload(function()
{
  var surveyMonth = moment().format('MMMM'); 
  var surveyYear = moment().format('YYYY'); 

  Qualtrics.SurveyEngine.setEmbeddedData('Survey Month', surveyMonth);
  Qualtrics.SurveyEngine.setEmbeddedData('Survey Year', surveyYear);
});

 

Hi @karen_split-pin 

Are the variables like Entry Date and Date of Birth correctly named are correctly named and match?
is the Moment.js formate correctly and match?


Leave a Reply