Are there errors in my javascript? | XM Community
Skip to main content

I’d like to calculate the difference between two dates: the current date, and the date of an upcoming appointment. I want the difference in days to be set as embedded data. So in the Survey Flow, I created the "Days" embedded data field and applied the following javascript to the survey question that collects the upcoming appointment date (QID190). However, the "Days" column is not populating when I run preview tests. Can you help me identify my error(s)?
Date_Subtraction.png

You can use the following code:

var startdmy = new Date("${q://QID8/ChoiceTextEntryValue/1}").getTime();
 var enddmy = new Date("${q://QID10/ChoiceTextEntryValue/2}").getTime();
    var diff = parseInt(enddmy - startdmy);
    var days = diff/1000/60/60/24;
    Qualtrics.SurveyEngine.setEmbeddedData("Days", days);


Thanks for the suggestion, Mishraji, but it didn't work. Could the issue be that one date includes hours and the other date does not?


CX_Research - That could be the reason. In the code that I shared, dates are in mm/dd/yyyy format.
Can you try to parseInt the diff in your code and try that?


Leave a Reply