Screen out based on date difference | XM Community
Skip to main content

Could you help to fix this script?

I need to calculate the number of days between showDate and current date. if the number of days is greater than 56 then screenOut is true else false.

Currently this is not working and populating false in all tests whether is > or <.

 

Qualtrics.SurveyEngine.addOnload(function() {

  /* Place your JavaScript here to run when the page loads */

  var showDate = "${e://Field/TagShowDate}"; // Replace with the actual embedded data

  var currentDate = new Date();

  var showDateObj = new Date(showDate);

  var daysDifference = Math.floor((currentDate - showDateObj) / (1000 * 60 * 60 * 24));

  if (daysDifference > 56) {

    Qualtrics.SurveyEngine.setEmbeddedData("screenOut", "true");

  } else {

    Qualtrics.SurveyEngine.setEmbeddedData("screenOut", "false");

  }

});

 

@Deepak 

Hi @praveengeorgeix ,

 

You can try this with getTime

 

var currentDate = new Date();

var showDateObj = new Date(showDate);

var currentTime = currentDate.getTime();

var showDateTime = showDateObj.getTime();

var daysDifference = Math.floor((currentTime - showDateTime) / (1000 * 60 * 60 * 24));


Thanks Jagadish


Hi @praveengeorgeix i have a similar issue but i am very new to Javascript and i dont know where/ how to start. If you can help me out,

  1. How should i create the ‘showdate’?
  2. where do i copy the above code to?

 

You can place it anywhere in the survey.

I pasted in the first question javascript area.

 

 

 


Hi @praveengeorgeix thanks for the quick response. 

I am completely new to all this. I don’t exactly understand what to do. If you could please explain step by step what i should do and please know that i have no experience with embedded logics etc. 

I think it’s best if i provide some context to my form so that it gives you a visual look on how my form looks like and how you can help me resolve this.

My survey is a conference support registration form and its about 8questions all in one block. On the 6th question, users will enter the date they will be attending a conference. If the date they enter is less than 30days from the current date they are completing the form, i want the form to terminate. 

 

What do i do first please?


As a first mandatory stem create 2 variables in embedded data.

  1. ConferenceDate
  2. screenOut

Both variables are case sensitive.

 

 

Then change the variable type as below from options in right corner of green box.

 

 

What do you mean by terminate.


I’m stuck on the “Then change the variable type as below from options in right corner of green box.” Where did you go to get that Options menu? 

 

Also to terminate mean i want to the survey to end for the user. 


@praveengeorgeix 


https://www.qualtrics.com/support/survey-platform/survey-module/survey-flow/standard-elements/embedded-data/#EmbeddedDataOptions

 

check under heading “embedded data option”


Okay so i have completed these steps you sent. 

What next? @praveengeorgeix 


could you share a screen shot of your date question?


see below

 


@praveengeorgeix 


click on the question then you will see javascript on left bottom as in screen shot.

 

 

Add this script below in the javascript.

 

 

Qualtrics.SurveyEngine.addOnload(function() {

    

  /* Place your JavaScript here to run when the page loads */

  var confDate = "${e://Field/Q6}"; // Replace with the actual Question

  var currentDate = new Date();

  var confDateObj = new Date(confDate);

  var daysDifference = Math.floor((confDateObj - currentDate) / (1000 * 60 * 60 * 24));

  if (daysDifference > 30) {

    Qualtrics.SurveyEngine.setEmbeddedData("screenOut", "true");

  } else {

    Qualtrics.SurveyEngine.setEmbeddedData("screenOut", "false");

  }

});

 


You need to change the Q7 and Q8 to a different block

and add a stop node based on branch as below.

This should work. I can see an error. I need to log out now. The foundation is correct.


Hi @praveengeorgeix I’m not sure if i did the right thing so please see below.

 

  1. If i understood you correctly, are you suggesting i change my previous question 8 from my screenshot( which was a text entry question type) to the screen shot you sent ( which is a the form field question type). I am unsure if you want me to change that question type or create a new question. Please clarify that for me.
  2. Anyway i went ahead to change the question type into a form field. After that, I copied the codes you sent over into the Javascript and saved. 
  3. That is what i have so far, kindly prompt me if it follows the logic you explained.
  4.  Also, I don’t understand what you mean by “You need to change the Q7 and Q8 to a different block”. Which question are you referring to as Q7? Also, do you mean i create a new block and move my Q8 into it?? Please clarify this statement.
  5. However i have tested it but it is not working. When testing i entered todays date 11/17/2023 hoping the survey will end but i still kept going. 

Below is screenshot of what my survey flow currently looks like

 


Leave a Reply