How do I subtract between two dates in a survey? | XM Community
Solved

How do I subtract between two dates in a survey?


I have two seperate questions in a survey that are dates. I would like to calculate the difference between these two dates as this is a component of determining a participant's eligibility. Does anyone know specific javascript code that I can utilize in order to calculate this difference and add it to the survey logic?
icon

Best answer by MohammedAli_Rajapkar 25 April 2018, 04:29

View original

20 replies

Userlevel 7
Badge +20
Hi @kducharme

If you are familiar with javascript then you can implement something similar to below.

https://stackoverflow.com/questions/2609513/jquery-calculate-day-difference-in-2-date-textboxes
Hey,

Thank you! I was just wondering- if I inset the text below into javascript...

var start = new Date("2010-04-01"),
end = new Date(),
diff = new Date(end - start),
days = diff/1000/60/60/24;

days;

How do I link it to the questions that have calendar dates into this? Also do I have to do anything into embedded data in survey field after I set this up in javascript?

Thank you for your help!

Qualtrics Newbie 🙂
Userlevel 7
Badge +27
You pipe the date values into your JavaScript. For example:
```
var start = new Date("${q://QID1/ChoiceTextEntryValue}");
```
You save your computed value to an embedded data field like this:
```
Qualtrics.SurveyEngine.setEmbeddedData("daysDiff",days);
```
Be sure to initialize your embedded data field in the survey flow if you want it included in your response data.
Userlevel 7
Badge +20
You will have to find the start date question id which you can do by using "inspect element" as shown in below URL: check for locating web element in chrome

http://www.softwaretestinghelp.com/locate-elements-in-chrome-ie-selenium-tutorial-7/

var start = jQuery("#replace_with_start_date_id").val();
var end = new Date(), (or if you are asking the end date in survey then jQuery("#replace_with_end_date_id").val();)

var diff = new Date(end - start),
var days = diff/1000/60/60/24;

Qualtrics.SurveyEngine.setEmbeddedData( "Datediff", days)

create a "Datediff" embedded data in survey flow.

Hope the above steps help!
Userlevel 2
Badge +1
If I have to calculate the difference in time between the two dates, how can we work that out?
Userlevel 7
Badge +27
> @AAnuragDAS said:
> If I have to calculate the difference in time between the two dates, how can we work that out?

I recommend you use moment.js.
Userlevel 2
Badge +1
@TomG I did look into moment.js but how will we be placing embedded field in the script. Basically I need to calculate the difference between current time and time which we receive in the embedded field. The format will be DD/MM/YYYY HH:MM:SS
Userlevel 7
Badge +27
> @AAnuragDAS said:
> @TomG I did look into moment.js but how will we be placing embedded field in the script. Basically I need to calculate the difference between current time and time which we receive in the embedded field. The format will be DD/MM/YYYY HH:MM:SS

Look at the documentation for moment.format() and moment.diff().
Badge +1

Hi TomG ,

I have tried this technique to pipe my question text into the formula and it is not working. Am I doing something wrong? I want the number of days between someone's last visit to campus

(
${q://QID61/ChoiceGroup/AllChoicesTextEntry}), and the current date. This is what I have:
var start = new Date(${q://QID61/ChoiceGroup/AllChoicesTextEntry}),
    end   = new Date(),
    diff  = new Date(end - start),
    days  = diff/1000/60/60/24
I put this in the Set Embed Data portion of the survey flow:
image.pngIt just returns the formula. Am I not entering this in the right place?

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/32778#Comment_32778The code is JavaScript. You have to add it to a question using "Add JavaScript..."

Badge +1

Hi
https://www.qualtrics.com/community/discussion/comment/32778#Comment_32778Hi TomG ,
That was helpful, but I'm still not seeing how that results in the number of days between the date.
var start = new Date(${q://QID61/ChoiceGroup/AllChoicesTextEntry}),
    end   = new Date(),
    diff  = new Date(end - start),
    days  = diff/1000/60/60/24
When I enter this into the JS editor, I keep getting errors.
image.pngI am using the Piped Text option to embed the Question ID into the var start parentheses. I remove the curly bracket, and then it says to remove the :. I remove that, and it says to remove the close curly bracket. It seems like it doesn't want any of the piped text for the question.
All I want is for the question to display a number, like 4 if the last time I was on campus was 12/13/20 and today is 12/17/20. I will then make that number question invisible and only view it as part of the dashboard on the backend.

Userlevel 7
Badge +27

The piped text has to be in quotes, such as "${q://QID61/ChoiceGroup/AllChoicesTextEntry}"

Badge

Similar question here except I'd like the difference between dates to be set as embedded data. So in Survey Flow, I created the "Days" embedded data field and applied the following js to my survey question (QID190). However, the "Days" column is not populating when I run preview tests. Can you help me identify my error(s)?
image.png

Userlevel 3
Badge +10

Is there a way to calculate this field with survey data already collected. I would like to know the difference in days between two dates as an indicator in a dashboard ... Thank you

Userlevel 1
Badge +1

I'm not a coder, I don't know javascript - but I am pretty savvy and can make things work but maybe the bigger question is why do we need these skills for things like a date field - I recently had to create a date/time field and was looking for someone to provide step by step instructions on how to do that - I got it figure out but why did I have to go through all that effort? I spent way too much time on this - now I need to calculate the number of days between the two fields - again - need to spend way to many hours trying to figure this out. If its a number or date field you should be able to EASILY create a calculation - this isn't rocket science and other survey platforms offer it - so Qualtrics - why can't you create a date field type or a date/time field type for every day users and make things easy?

Userlevel 7
Badge +27

https://community.qualtrics.com/XMcommunity/discussion/comment/46538#Comment_46538You should submit a product idea and/or vote for existing product ideas that address date calculations.

Userlevel 1
Badge +1

TomG - thanks - I've submitted as an product idea

Userlevel 7
Badge +27

Hi spreik1 , if you still need, I have a QSF with examples of calculations between 2 date fields in days. Will just want to update the QIDs in the JS and survey flow for what matches your questions.
DateCalculations_Days.qsf

Userlevel 3
Badge +11

Hi everyone, Is there a way to calculate this field with two embedded fields Like date1 and date2, instead of two questions? Thank you!

Userlevel 3
Badge +10

Hi ManuelaPosso13
Have you test this calculation in your surveyflow with piped text ?
$e{ ${q://QID?(DATE2)/ChoiceTextEntryValue} - ${q://QID(DATE1)/ChoiceTextEntryValue} }

Leave a Reply