Need help calculating the number of days between two dates | XM Community
Skip to main content

Hello,

I have a survey with two questions asking the respondent to enter the start date and end date. Both questions are Text entry question types. The format for the response is mm/dd/yyyy. I would like the total number of days between the two dates to show in my last question that is set as Text/Graphic question type. 

I’ve tried using using this JavaScript below:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
    var DateTime = luxon.DateTime;

   
    var startDate = DateTime.fromFormat("${q://QID6/ChoiceTextEntryValue}", "MM/dd/yyyy");
    var endDate = DateTime.fromFormat("${q://QID7/ChoiceTextEntryValue}", "MM/dd/yyyy");

  
    var difference = endDate.diff(startDate, i'years']).toObject();

   
    Qualtrics.SurveyEngine.setEmbeddedData('creditDuration', difference.days);
});
 

However nothing shows for the result.  Is there a way of calculating the number of days between the two dates entered?

Any help is much appreciated!

Thank you,

Brkwms

Three things:

  1. If you are using Simple layout you need to use setJSEmbeddedData and change field name to be prefixed by __js_ in the survey flow and any piping.
  2. For the date pipes to work, the script has to be on a page after the date questions. Given that, it would make more sense to use addOnload instead of addOnPageSubmit.
  3. Finally, you can’t pipe the result until the page after the JS. 

Thanks for the information. I’ve updated the JS to: 

Qualtrics.SurveyEngine.addOnload(function()
{
var DateTime = luxon.DateTime;

   
    var startDate = DateTime.fromFormat("${q://QID6/ChoiceTextEntryValue}", "MM/dd/yyyy");
    var endDate = DateTime.fromFormat("${q://QID7/ChoiceTextEntryValue}", "MM/dd/yyyy");

  
    var difference = endDate.diff(startDate, f'days']).toObject();

   
    Qualtrics.SurveyEngine.setJSEmbeddedData('creditDuration', difference.days);


});

I have the JavaScript after the date questions and the page before where I need to pipe the results. I have the field for the results: Credit Prior Years: ${e://Field/__js_creditDuration}

Still no luck, what else am I missing?


Hmmm…

  1. Have you loaded Luxon in either the survey header or footer?
  2. Are you sure the date pipes are correct?

Yes, I have “<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/luxon@3.4/build/global/luxon.min.js"></script>” in the header under Look and Feel for a different date question. 

As far as the date pipes, do I need to have the start and end dates listed in the Survey Flow as Embedded Data? 

 


You don’t need start and end date as embedded data fields.

The code you posted works for me, so I’m guessing your pipes are incorrect (Start and End date questions aren’t really QID6 and QID7).


Here is a screen shot of the start and end dates if it helps. I apologize, I am still a newbie at this. 

 

Here is where I have the JS 

And this is where I have the piped result for Credit Prior Years

 


The question label ‘Q6’ is not the same thing as the question id ‘QID6’. I think that may be your problem. Use ‘Piped Text...’ to pipe the answers of Q6 and Q7 into the question text (the question with your JavaScript). Then copy the pipe strings into your JavaScript.


Thank you for your help. I was able to get it to work using this JS:

Qualtrics.SurveyEngine.addOnload(function() {
    $(document).ready(function() {
        
        var getDateDifference = function() {
            var startDateStr = "${q://QID6/ChoiceTextEntryValue}";
            var endDateStr = "${q://QID7/ChoiceTextEntryValue}";

            if (startDateStr && endDateStr) {
                var startDate = luxon.DateTime.fromFormat(startDateStr, "MM/dd/yyyy");
                var endDate = luxon.DateTime.fromFormat(endDateStr, "MM/dd/yyyy");

                // Calculate the difference in days
                var diffInDays = endDate.diff(startDate, 'days').days;

                // Update the span with id 'days-difference' with the calculated difference
                $('#days-difference').text(diffInDays.toFixed(0));
            }
        };

        // Initial calculation
        getDateDifference();

        // Register event handlers for changes in QID6 and QID7 inputs
        $('input    });
});

 

And updating the HTML in the question for the display: 

<div>Retirement Eligibility Summary&nbsp;</div>
<div><br></div>
<div>Current Date: ${date://CurrentDate/SL}</div>
<div><br></div>
<div>Date Of Birth: ${e://Field/Date%20of%20Birth}</div>
<div><br></div>
<div>Service Date: ${e://Field/__js_age}&nbsp;</div>
<div><br></div>
<div>Age: ${e://Field/__js_service}</div>
<div><br></div>
<div>CPY Start Date: ${q://QID6/ChoiceTextEntryValue}</div>
<div><br></div>
<div>CPY End Date: ${q://QID7/ChoiceTextEntryValue}<br></div>
<div><br></div>
<div>Credit Prior Years Total Days: <span id="days-difference"></span></div>
<div><br></div>


 


Leave a Reply