Search code examples
javascriptdatequaltrics

Javascript to add future dates to embedded data in Qualtrics


I'm being asked to integrate JavaScript into Qualtrics to accomplish the following three steps:

  1. Get the current date in UTC format and convert to ISO 8601.
  2. Use the current date in the prior step to create a future date (i.e., 3 months from now).
  3. Assign the value of the date generated in #2 to an embedded data field.

Note: This isn't in my wheelhouse and I'm having quite a bit of trouble accomplishing this task.

Here's a snippet of code that I've generated thus far:

//add 3 months from current date;
var someDateM3 = new Date();
var numberOfDaysToAdd = 90;
someDateM3.setDate(someDateM3.getDate() + numberOfDaysToAdd);
var month3 = someDateM3.toISOString();

//assign value of month3 to embedded data field 'month3';
Qualtrics.SurveyEngine.addEmbeddedData( 'month3', "${e://Field/month3}");

Am I on the right track? I lack the training and skillset required to complete this seemingly straightforward task. Any advice would be incredibly helpful.


Solution

  • I think your date calculation is fine. The problem is with saving it to an embedded data variable. That should be:

    Qualtrics.SurveyEngine.setEmbeddedData('month3', month3);
    

    You need to define the embedded data variable month3 in the survey flow prior to the block that contains the question with your date calculation JavaScript.