Displaying day of the week | XM Community
Question

Displaying day of the week

  • 9 January 2024
  • 3 replies
  • 42 views

Badge +1

Hi,

 

I have a survey question which asks for a date on which the customer would like to have a follow up call. Is there anyway to display if the given date fall under public holidays or weekends.

For example, if the customer chooses 14th Jan 2024 then the survey display error as Sunday. (it can also be a notification in next question.)


3 replies

Badge +30

You can try something like below 

var date = new Date();
switch(date.getDay()){
case 0: alert("sunday!"); break;
case 6: alert("saturday!"); break;
default: alert("any other week day");
}

 

Userlevel 7
Badge +33

if you are looking for below output.
 

Then you can follow below steps.


First create 3 variables, first one is text/graphic, second and third is OE. in which second will be validate as date format and third will have default choice as 0. please refer below screenshots.
 

Validate as date format Q30. refer below screenshot


Q31 hidden variable: no validation, no add requirement and default choice as 0. refer below screenshot

 

Add below JS codes at Q30: read comment next to each line of code for more clarity. ** Please note, updated the ID and class as per your setup.
 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
jQuery("#QID31").hide() // Hide "hidden variable Q31"
jQuery('#NextButton').click(function(){
var dateX= jQuery('.QR-QID30').val(); // Capture date
const d = new Date(dateX);
let day = d.getDay(); // get week day from date in numbers from 0 to 6
var arrDay=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] // create arrary based on date number 0 to 6
var DayS = arrDay[day] // Capture day from array
if(DayS == "Sunday")
{
jQuery(".QR-QID31").val('1'); // if day is Sunday then set 1
}
else{
jQuery(".QR-QID31").val('0'); // if not Sunday then set 0
}
Qualtrics.SurveyEngine.setEmbeddedData( 'Day', DayS); // capture day into embedded field
});
});

 

Then add custom validation at Q32.

 

Badge +1

@ArunDubey Thanks for your response.  I tried this but it is not working. I might be making mistake somewhere. Would you mind sharing the survey in qsf format ?

Leave a Reply