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

How do I calculate the duration between two text entry dates and save as an embedded data?

  • October 10, 2022
  • 8 replies
  • 873 views

Forum|alt.badge.img

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



8 replies

Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+44
  • 1549 replies
  • October 10, 2022

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('input[type=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:



Forum|alt.badge.img
  • Author
  • 1 reply
  • October 12, 2022

Thank you Deepak.


SuhasM
Qualtrics Employee
Forum|alt.badge.img+16
  • Qualtrics Employee
  • 114 replies
  • October 12, 2022

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 🙌


Forum|alt.badge.img
  • Level 1 ●
  • 4 replies
  • May 12, 2023

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


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+44
  • 1549 replies
  • May 12, 2023
Nazuko wrote:

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("[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('input[type=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);




});

});

Hope it helps! 


Forum|alt.badge.img
  • Level 1 ●
  • 4 replies
  • May 12, 2023

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('input[type=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.


Forum|alt.badge.img
  • Level 1 ●
  • 4 replies
  • May 12, 2023

@Deepak see above message, pls

Thanks


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+44
  • 1549 replies
  • May 12, 2023

Use the code mentioned here 

Deepak wrote:
Nazuko wrote:

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("[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('input[type=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);




});

});

Hope it helps! 

 


Leave a Reply