Modifying Javascript Code to Add Days to Date Entered | XM Community
Skip to main content
Solved

Modifying Javascript Code to Add Days to Date Entered

  • November 15, 2021
  • 3 replies
  • 296 views

Forum|alt.badge.img+3

Hello! I'm a complete newbie to JavaScript coding, and I need a little help modifying a code I have to serve a slightly different purpose. The code below uses the date entered for the question, adds 10 days, and then sets the new date as embedded data. (A friend helped me with this part!) I need to use the same date and add 5 days to create a different embedded data point. I reviewed several other discussions and tried modifying the code on my own, but instead of adding 5 days to the answer entered, it added 5 days to the original calculation (answer entered + 10). So . . . how would I modify this to get it to calculate Date + 10 AND Date + 5 separately (and set both as different embedded data)? (I'm also OK with setting up the Date + 5 as part of another question, just pulling the answer from the previous question . . . but I wasn't sure exactly what to change to make that work.) Thanks in advance for your help!

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
jQuery(".InputText:eq(0)").on("change", function(){
var theDate = new Date(jQuery(".InputText:eq(0)").val())
   var futureDateRaw = new Date(theDate.setDate(theDate.getDate() + 10));
console.log(futureDateRaw)
   var futureDate = (futureDateRaw.getMonth() + 1) + '/' + futureDateRaw.getDate() + '/' + futureDateRaw.getFullYear()

  Qualtrics.SurveyEngine.setEmbeddedData("DatePlus10", futureDate);
})

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

Best answer by Tom_1842

Hi there, if you still need, I was able to put this in place by first creating the Embedded Data fields of "DatePlus5" and "DatePlus10" and putting them at the top of the Survey Flow. Then, on the Text Entry question that collects the date, the following can be added to the OnReady section of the question's JavaScript:
var qid = this.questionId;

jQuery("#"+qid+" .InputText:eq(0)").on("change", function() {

var theDate = new Date(jQuery("#"+qid+" .InputText:eq(0)").val());

var futureDateRaw5 = new Date(theDate);
futureDateRaw5.setDate(futureDateRaw5.getDate()+5);

var futureDateRaw10 = new Date(theDate);
futureDateRaw10.setDate(futureDateRaw10.getDate()+10);

console.log(futureDateRaw5);
console.log(futureDateRaw10);

var futureDate5 = (futureDateRaw5.getMonth() + 1) + '/' + futureDateRaw5.getDate() + '/' + futureDateRaw5.getFullYear();
var futureDate10 = (futureDateRaw10.getMonth() + 1) + '/' + futureDateRaw10.getDate() + '/' + futureDateRaw10.getFullYear();

Qualtrics.SurveyEngine.setEmbeddedData("DatePlus5", futureDate5);
Qualtrics.SurveyEngine.setEmbeddedData("DatePlus10", futureDate10);

});

View original

3 replies

Tom_1842
Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • Level 8 ●●●●●●●●
  • 876 replies
  • Answer
  • November 9, 2022

Hi there, if you still need, I was able to put this in place by first creating the Embedded Data fields of "DatePlus5" and "DatePlus10" and putting them at the top of the Survey Flow. Then, on the Text Entry question that collects the date, the following can be added to the OnReady section of the question's JavaScript:
var qid = this.questionId;

jQuery("#"+qid+" .InputText:eq(0)").on("change", function() {

var theDate = new Date(jQuery("#"+qid+" .InputText:eq(0)").val());

var futureDateRaw5 = new Date(theDate);
futureDateRaw5.setDate(futureDateRaw5.getDate()+5);

var futureDateRaw10 = new Date(theDate);
futureDateRaw10.setDate(futureDateRaw10.getDate()+10);

console.log(futureDateRaw5);
console.log(futureDateRaw10);

var futureDate5 = (futureDateRaw5.getMonth() + 1) + '/' + futureDateRaw5.getDate() + '/' + futureDateRaw5.getFullYear();
var futureDate10 = (futureDateRaw10.getMonth() + 1) + '/' + futureDateRaw10.getDate() + '/' + futureDateRaw10.getFullYear();

Qualtrics.SurveyEngine.setEmbeddedData("DatePlus5", futureDate5);
Qualtrics.SurveyEngine.setEmbeddedData("DatePlus10", futureDate10);

});


Forum|alt.badge.img+3
  • Author
  • 3 replies
  • November 11, 2022

JoseS
Former XM Community Team Member
Forum|alt.badge.img+20
  • Former XM Community Team Member
  • 447 replies
  • December 13, 2022

Tom_1842 Thank you for tackling this older post and giving such a detailed response, we really appreciate having you in the community! 😁