Subtracting days from date using Moment.js | XM Community
Skip to main content

Hello!

 

I’m trying to figure out how to subtract days from an input date from flatpickr using Moment.js’s subtract function, and display that in a subsequent survey question.

The following is the JavaScript I used to ask participants to select one of the days the past 3 days from the current date using flatpickr:

 

Now, I would like to create a new variable which is the day prior to what was selected in the next question. For example, if the date 2023-08-23 was selected, I want to create an output of 2023-08-22 in the next question using Moment.js:

How do I properly create a variable which subtracts a day from the previously inputted date, and how do I display that date into the question?

 

I’m not familiar with coding, so any guidance or help would be greatly appreciated!

 

 

 

You can use
Qualtrics.SurveyEngine.setEmbeddedData("fieldName", value); to save it as an EmbeddedData

And use Pip-text ${e://Field/fieldName} to display it

 


Hi,

Add an ED field named date2 at the beginning of your survey flow and add this code to your flatpickr question’s javascript:

Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
let d = new Date(jQuery("#"+this.questionId+" .InputText").val());
d.setDate(d.getDate() -1);
var d2 = d.toISOString().slice(0, 10);
Qualtrics.SurveyEngine.setEmbeddedData("date2", d2);

});

Pipe in your date in the subsequent question using 

${e://Field/date2}

 


Thank you both for your answers! I greatly appreciate it.

I was able to create an embedded variable and pipeline it properly into the question.


Leave a Reply