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

Are there errors in my javascript?

  • May 10, 2021
  • 3 replies
  • 19 views

Forum|alt.badge.img

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

3 replies

Mishraji
Level 4 ●●●●
Forum|alt.badge.img+18
  • Level 4 ●●●●
  • May 17, 2021

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);


Forum|alt.badge.img

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?


Mishraji
Level 4 ●●●●
Forum|alt.badge.img+18
  • Level 4 ●●●●
  • May 18, 2021

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?