Comparing dates in Branch Logic | XM Community
Skip to main content
Solved

Comparing dates in Branch Logic


Forum|alt.badge.img+1

Hello!
I have made a two-part survey, where the second survey must be completed 4-5 days after completion of the original survey. If the participant comes back early, I want to show a message saying 'Too early!', and if they're too late 'Too late!'. If they're on time, the survey continues.
In the first survey, I create embedded data of the current date, and the next 6 days, respectively:
Day0 = ${date://CurrentDate/DM}
Day1 = ${date://OtherDate/DM/+1%20day}
...
Day6 = ${date://OtherDate/DM/+6%20day}

These are piped to the second survey, where the ppt authenticates using a LoginID, and an embedded data field, 'Today', is created: Day0 = ${date://CurrentDate/DM}
Then I have three branches of logic, in this order.

1) If 'Today' is equal to or less than 'Day 3' then "Too early!"
2) If 'Today' is equal to 'Day 4' OR 'Day 5 ' then continue to survey.
3) If 'Today' is equal or greater than 'Day 6' then "Too late!"

At the moment, all my participants are being sent to "Too late!" and I'm not sure why?All the ED fields seem to be piping perfectly from one study to another.
Some ideas:

  • Could it be due to the dates from survey 1 are saved as strings, while the 'Today' field is a date?

  • Or perhaps because I've used the verbose 'Day Month' format, rather than the mm/dd/yyyy format for the dates? Perhaps greater/less than calculations don't work on this?

If anyone can shed light on this, I would hugely appreciate it!
Thanks in advance :)



Best answer by Rudi

Hi a_e_scott,
my approach would be to handle this with javascript using the Date function
following this approach you would need only two embedded data fields
and here come the steps

  1. create two embedded data fields

  2. lastSurveyFinished -> holds string format mm/dd/yyyy

  3. currentSurveyOpen -> stores three values you can base your branch on

  4. add a text/graphic question in your survey. make sure that the embedded data fields are initiated before this question in the survey flow

  5. add the below script to the new question


//function splitting the string storing your finishing date of the previous survey
//place on top of the javaScript above the addOnload function
function createDateFromString(dteString){
var dte = new Date(dteString.split("/")[2], dteString.split("/")[0]-1, dteString.split("/")[1]);
return dte;
}
Qualtrics.SurveyEngine.addOnload(function()
{

let today = new Date;
let strLastSurveyStartDate = "${e://Field/lastSurveyFinished}"
let dteLastSurveyStartDate = createDateFromString(strLastSurveyStartDate)
console.log(strLastSurveyStartDate)
let currentSurveyOpen


console.log(dteLastSurveyStartDate)

let dteDifference = parseInt((today.getTime() - dteLastSurveyStartDate.getTime())/(1000 * 3600 * 24))

console.log(dteDifference)

if(dteDifference <= 3){
currentSurveyOpen = 1; //too early

}else if(dteDifference >=4 && dteDifference <=5){
currentSurveyOpen = 2; //survey accessible

}else if(dteDifference >5 ){
currentSurveyOpen = 3; //survey closed

}

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

this.clickNextButton()
});

Best regards

Rudi

View original

5 replies

Rudi
QPN Level 3 ●●●
Forum|alt.badge.img+16
  • QPN Level 3 ●●●
  • 162 replies
  • March 29, 2022

Hi,

I think both variables must be dates and have the same format in order to be comparable. Have you tried to set the variable type of your embedded data field to date in the options?

image.png
Best regards
Rudi


Forum|alt.badge.img+1
  • Author
  • 8 replies
  • March 30, 2022

Rudi, thanks for your response!
I have set all the embedded data to the 'Date' variable type, in both the first survey and the second survey. They are definitely in the identical format, which I have now changed to mm/dd/yyyy. Unfortunately, still no luck.
My participants are all sent to the final branch; if TodayDate is equal to or greater than Date6 = 'Too late!'
Anything else I can try?


Rudi
QPN Level 3 ●●●
Forum|alt.badge.img+16
  • QPN Level 3 ●●●
  • 162 replies
  • Answer
  • March 31, 2022

Hi a_e_scott,
my approach would be to handle this with javascript using the Date function
following this approach you would need only two embedded data fields
and here come the steps

  1. create two embedded data fields

  2. lastSurveyFinished -> holds string format mm/dd/yyyy

  3. currentSurveyOpen -> stores three values you can base your branch on

  4. add a text/graphic question in your survey. make sure that the embedded data fields are initiated before this question in the survey flow

  5. add the below script to the new question


//function splitting the string storing your finishing date of the previous survey
//place on top of the javaScript above the addOnload function
function createDateFromString(dteString){
var dte = new Date(dteString.split("/")[2], dteString.split("/")[0]-1, dteString.split("/")[1]);
return dte;
}
Qualtrics.SurveyEngine.addOnload(function()
{

let today = new Date;
let strLastSurveyStartDate = "${e://Field/lastSurveyFinished}"
let dteLastSurveyStartDate = createDateFromString(strLastSurveyStartDate)
console.log(strLastSurveyStartDate)
let currentSurveyOpen


console.log(dteLastSurveyStartDate)

let dteDifference = parseInt((today.getTime() - dteLastSurveyStartDate.getTime())/(1000 * 3600 * 24))

console.log(dteDifference)

if(dteDifference <= 3){
currentSurveyOpen = 1; //too early

}else if(dteDifference >=4 && dteDifference <=5){
currentSurveyOpen = 2; //survey accessible

}else if(dteDifference >5 ){
currentSurveyOpen = 3; //survey closed

}

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

this.clickNextButton()
});

Best regards

Rudi


Forum|alt.badge.img+1
  • Author
  • 8 replies
  • April 5, 2022

Rudi
Thanks so much for this elegant solution. I just implemented it, and it seems to work perfectly.
😻


Rudi
QPN Level 3 ●●●
Forum|alt.badge.img+16
  • QPN Level 3 ●●●
  • 162 replies
  • April 5, 2022

a_e_scott,

great to hear it works for you

Best regards

Rudi


Leave a Reply