Display Question if dates in previous question are > 4 years apart | XM Community
Skip to main content

Hi,

 

I’m trying to display a question if the start date and end date of a medical degree is greater than 4 years apart. How would I do that?

 

I’ve tried some javascript, but no luck. Dates are provided in a form field question as mm/dd/yyyy.

 

Qualtrics.SurveyEngine.addOnload(function()
{
    var answer = "${q://QID11/ChoiceTextEntryValue/1}"; // Replace QID1 with your actual Question ID
    var lastFourChars = answer.slice(-4);
    Qualtrics.SurveyEngine.setEmbeddedData('startyears', lastFourChars);
    var answer2 = "${q://QID11/ChoiceTextEntryValue/2}"; // Replace QID1 with your actual Question ID
    var lastFourChars2 = answer.slice(-4);
    Qualtrics.SurveyEngine.setEmbeddedData('endyears', lastFourChars2);

});
 

Use luxon (load in survey header or footer). Then add this JS to your form field question (start date is the first field, end date is the second field):

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var dates = jQuery(this.getQuestionContainer()).find("input[type=text]");
var startDate = DateTime.fromFormat(dates.eq(0).val(),"MM/dd/yyyy");
var endDate = DateTime.fromFormat(dates.eq(1).val(),"MM/dd/yyyy");
var end = endDate.diff(startDate, ['years']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('endyears',end.years);
});

endyears will be a decimal number (e.g., 3.821).  For display logic purposes the embedded data field will be __js_endyears. Also if you want to save endyears in your response data add __js_endyears to the survey flow prior to your form question. The code will work with any layout, but if you use Simple layout you also need to load jQuery.


Use luxon (load in survey header or footer). Then add this JS to your form field question (start date is the first field, end date is the second field):

Thank you, this worked in the preview! If it doesn’t work in the live test, I’ll come back to this thread.

For anyone else:

To load Luxon, I added the following to the header in the Look & Feel section:

<script src="https://cdn.jsdelivr.net/npm/luxon@3.4/build/global/luxon.min.js"></script>

(Source: TomG 6mo ago)

The header in the Look & Feel section

 


@sthom55, thanks for confirming it worked! Hope all goes well in the live test.


Leave a Reply