How do I calculate the duration between two text entry dates and save as an embedded data? | XM Community
Skip to main content

I have a form field question within a survey that has two text entry values for first help date and last help date (both include Date validation yyyy/mm/dd). With the help of other discussion threads, I implemented the following JavaScript code on the survey question and added an embedded data field in the survey workflow called, Days.
When testing through survey preview, the "Days" field does not populate as expected.
JavaScript Code.JPGSurveyFlow.JPG



amy_s
I would recommend including a calendar instead of validation. I have used "Difference_In_Days" to capture the final value. Hence , for the same you can include the below code:
Qualtrics.SurveyEngine.addOnload(function()
{



jQuery("[id='QR~QID3~3']").attr("placeholder","mm/dd/yyyy");

jQuery("#ui-datepicker-div").remove(); 

jQuery(""id='QR~QID3~3']").flatpickr({







dateFormat: "m/d/Y",

minDate:"01/01/1900",

maxDate:new Date().fp_incr(0)});



jQuery(""id='QR~QID3~4']").attr("placeholder","mm/dd/yyyy");

jQuery("#ui-datepicker-div").remove(); 

jQuery("(id='QR~QID3~4']").flatpickr({







dateFormat: "m/d/Y",

minDate:"01/01/1900",

maxDate:new Date().fp_incr(0)});

});




Qualtrics.SurveyEngine.addOnReady(function()

{



jQuery('inpututype=text]').change(function() {




var date12 = jQuery("(id='QR~QID3~3']").val();

var date21 = jQuery("(id='QR~QID3~4']").val();

var date1 = new Date(date12);

var date2 = new Date(date21);




var Difference_In_Time = date2.getTime() - date1.getTime();

  

// To calculate the no. of days between two dates

var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);

  Qualtrics.SurveyEngine.setEmbeddedData("Difference_In_Days", Difference_In_Days);

//To display the final no. of days (result)

console.log("Total number of days between dates 
"

        + date1 + "
and


        + date2 + " is:


        + Difference_In_Days);

console.log(date12);

console.log(date21);

});

});
And include this in header:



Thank you Deepak.


Wow! Thank you very much for taking the time to create the custom code solution Deepak! It means the world to our team with your constant impact to the community 🙌


Hi @Deepak 

 

I entered the above code in JS to the question :”Please provide your year you were born.” but it gives me this error :

See my embedded data 

Can you tell me what I did wrong? 

Thank you


Hi @Deepak 

 

I entered the above code in JS to the question :”Please provide your year you were born.” but it gives me this error :

See my embedded data 

Can you tell me what I did wrong? 

Thank you

@Nazuko  I believe you added this line as well (And include this in header:)

Code ends above this. Also, you need to update the Question Id’s in code as per your survey.

Try this

Qualtrics.SurveyEngine.addOnload(function()
{



jQuery("bid='QR~QID3~3']").attr("placeholder","mm/dd/yyyy");

jQuery("#ui-datepicker-div").remove();

jQuery("bid='QR~QID3~3']").flatpickr({

dateFormat: "m/d/Y",

minDate:"01/01/1900",

maxDate:new Date().fp_incr(0)});

jQuery("bid='QR~QID3~4']").attr("placeholder","mm/dd/yyyy");

jQuery("#ui-datepicker-div").remove();

jQuery("bid='QR~QID3~4']").flatpickr({


dateFormat: "m/d/Y",

minDate:"01/01/1900",

maxDate:new Date().fp_incr(0)});

});




Qualtrics.SurveyEngine.addOnReady(function()

{



jQuery('inputjtype=text]').change(function() {




var date12 = jQuery("1id='QR~QID3~3']").val();

var date21 = jQuery("2id='QR~QID3~4']").val();

var date1 = new Date(date12);

var date2 = new Date(date21);

var Difference_In_Time = date2.getTime() - date1.getTime();



// To calculate the no. of days between two dates

var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);

Qualtrics.SurveyEngine.setEmbeddedData("Difference_In_Days", Difference_In_Days);




});

});

Hope it helps! 


Qualtrics.SurveyEngine.addOnload(function()
{

jQuery("[id='QR~QID7~3']").attr("placeholder","mm/dd/yyyy");

jQuery("#ui-datepicker-div").remove(); 

jQuery(""id='QR~QID7~3']").flatpickr({

dateFormat: "m/d/Y",

minDate:"01/01/1900",

maxDate:new Date().fp_incr(0)});

jQuery(""id='QR~QID7~4']").attr("placeholder","mm/dd/yyyy");

jQuery("#ui-datepicker-div").remove(); 

jQuery("(id='QR~QID7~4']").flatpickr({


dateFormat: "m/d/Y",

minDate:"01/01/1900",

maxDate:new Date().fp_incr(0)});

});


Qualtrics.SurveyEngine.addOnReady(function()

{

jQuery('inpututype=text]').change(function() {


var date12 = jQuery("(id='QR~QID7~3']").val();

var date21 = jQuery("(id='QR~QID7~4']").val();

var date1 = new Date(date12);

var date2 = new Date(date21);

var Difference_In_Time = date2.getTime() - date1.getTime();

// To calculate the no. of days between two dates

var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);

  Qualtrics.SurveyEngine.setEmbeddedData("Difference_In_Days", Difference_In_Days);


});

});
And include this in header:

I updated but I received the same message.


@Deepak see above message, pls

Thanks


Use the code mentioned here 

Hi @Deepak 

 

I entered the above code in JS to the question :”Please provide your year you were born.” but it gives me this error :

See my embedded data 

Can you tell me what I did wrong? 

Thank you

@Nazuko  I believe you added this line as well (And include this in header:)

Code ends above this. Also, you need to update the Question Id’s in code as per your survey.

Try this

Qualtrics.SurveyEngine.addOnload(function()
{



jQuery("
jQuery("#ui-datepicker-div").remove();

jQuery("
dateFormat: "m/d/Y",

minDate:"01/01/1900",

maxDate:new Date().fp_incr(0)});

jQuery("
jQuery("#ui-datepicker-div").remove();

jQuery("

dateFormat: "m/d/Y",

minDate:"01/01/1900",

maxDate:new Date().fp_incr(0)});

});




Qualtrics.SurveyEngine.addOnReady(function()

{



jQuery('input>type=text]').change(function() {




var date12 = jQuery("eid='QR~QID3~3']").val();

var date21 = jQuery("eid='QR~QID3~4']").val();

var date1 = new Date(date12);

var date2 = new Date(date21);

var Difference_In_Time = date2.getTime() - date1.getTime();



// To calculate the no. of days between two dates

var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);

Qualtrics.SurveyEngine.setEmbeddedData("Difference_In_Days", Difference_In_Days);




});

});

Hope it helps! 

 


Leave a Reply