Custom Calendar-Only Having one day available to select | XM Community
Skip to main content
Solved

Custom Calendar-Only Having one day available to select


Forum|alt.badge.img+2

Hi All, I have two date selection questions in my survey where I would like one of them to only have respondents be able to select Saturdays and the other to only be able to select every other Sunday. Is there a code for this? And how would I input this code? Also, can I add quotas so only a certain number of respondents can select each day? I appreciate your help as I am relatively new to Qualtrics. Thank you in advance!

Best answer by JesperAndersen

Just in cases anyone needs the complete JS, this is tested and working:

Qualtrics.SurveyEngine.addOnReady(function()
{
    function getAllSaturdays(year) {
      const saturdays = [];
      const date = new Date(year, 0, 1); // January 1st of the given year
      while (date.getFullYear() === year) {
        if (date.getDay() === 6) { // Saturday is day number 6 (0 is Sunday)
          const dateString = date.toISOString().slice(0, 10); // Format the date as "YYYY-MM-DD"
          saturdays.push(dateString);
        }
        date.setDate(date.getDate() + 1); // Move to the next day
      }
      return saturdays;
    }

    const allSaturdays2023 = getAllSaturdays(2023);
    
    jQuery("#"+this.questionId+" .InputText").flatpickr({
    dateFormat: "Y-m-d",
    minDate: new Date(),
    enable: allSaturdays2023        
    });

});

View original

15 replies

JesperAndersen
QPN Level 4 ●●●●
Forum|alt.badge.img+11
  • QPN Level 4 ●●●●
  • 92 replies
  • April 27, 2023

I would look at flatpickr. This is very flexible, and I think it can be set up to do what you are looking for,


JesperAndersen
QPN Level 4 ●●●●
Forum|alt.badge.img+11
  • QPN Level 4 ●●●●
  • 92 replies
  • May 5, 2023

Please try the following:


To make it work, you to add this code below in the Look & Feel > General > Header > Source View:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>

This makes sure you can access and use flatpickr from your questions.

In the question, you then need to add this code in the onReady function in the JS of a Text Entry question:

Use the below code in the text entry question JavaScript.

Qualtrics.SurveyEngine.addOnReady(function()
{
    jQuery("#"+this.questionId+" .InputText").flatpickr({
    dateFormat: "Y-m-d H:i",
    minDate: new Date(),
    enable: ["2023-05-06", "2023-05-13", "2023-05-20", new Date(2023, 5, 6) ]        
    });
});

You will have to specify all the dates you want to be enabled, but at least it works :)


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 8 replies
  • May 5, 2023

What does it mean if there is no source view?


JesperAndersen
QPN Level 4 ●●●●
Forum|alt.badge.img+11
  • QPN Level 4 ●●●●
  • 92 replies
  • May 5, 2023

It is there 😀

Look for the <> icon. 

When you see the text in source mode, then you are viewing the HTML version of the page


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 8 replies
  • May 5, 2023

When I add javascript, I see the source mode. What is the next step from there?

 


JesperAndersen
QPN Level 4 ●●●●
Forum|alt.badge.img+11
  • QPN Level 4 ●●●●
  • 92 replies
  • May 5, 2023

You should just add the two line above in the header. The Javascript should go in the Javascript part of the Text Entry question.


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 8 replies
  • May 10, 2023

Thanks! It worked.

How do I go about only having Saturdays or Sundays as options and only having a certain amount of Saturdays and Sundays being available to be selected?


JesperAndersen
QPN Level 4 ●●●●
Forum|alt.badge.img+11
  • QPN Level 4 ●●●●
  • 92 replies
  • May 10, 2023

Did you copy the code above? Then you should see only 2 days to be able to be selected.

If you look at this code:

Qualtrics.SurveyEngine.addOnReady(function()
{
    jQuery("#"+this.questionId+" .InputText").flatpickr({
    dateFormat: "Y-m-d H:i",
    minDate: new Date(),
    enable: ["2023-05-06", "2023-05-13", "2023-05-20", new Date(2023, 5, 6) ]        
    });
});

The bold part is the dates, so you will need to define the full range of dates you wish to be available in that format.


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 8 replies
  • May 10, 2023

Is there a way to make it every Saturday without having to list them all?


JesperAndersen
QPN Level 4 ●●●●
Forum|alt.badge.img+11
  • QPN Level 4 ●●●●
  • 92 replies
  • May 10, 2023

I found this:

function getAllSaturdays(year) {
  const saturdays = [];
  const date = new Date(year, 0, 1); // January 1st of the given year
  while (date.getFullYear() === year) {
    if (date.getDay() === 6) { // Saturday is day number 6 (0 is Sunday)
      const dateString = date.toISOString().slice(0, 10); // Format the date as "YYYY-MM-DD"
      saturdays.push(dateString);
    }
    date.setDate(date.getDate() + 1); // Move to the next day
  }
  return saturdays;
}

const allSaturdays2023 = getAllSaturdays(2023);
 


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 8 replies
  • May 10, 2023

I put this in the JS and it didn’t work. The date picker did not come up when clicking into the text box

 


JesperAndersen
QPN Level 4 ●●●●
Forum|alt.badge.img+11
  • QPN Level 4 ●●●●
  • 92 replies
  • May 10, 2023

Did you just add it like above, because that won't work.

Do you understand the code at all?


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 8 replies
  • May 10, 2023

I got it. Thanks


JesperAndersen
QPN Level 4 ●●●●
Forum|alt.badge.img+11
  • QPN Level 4 ●●●●
  • 92 replies
  • Answer
  • May 11, 2023

Just in cases anyone needs the complete JS, this is tested and working:

Qualtrics.SurveyEngine.addOnReady(function()
{
    function getAllSaturdays(year) {
      const saturdays = [];
      const date = new Date(year, 0, 1); // January 1st of the given year
      while (date.getFullYear() === year) {
        if (date.getDay() === 6) { // Saturday is day number 6 (0 is Sunday)
          const dateString = date.toISOString().slice(0, 10); // Format the date as "YYYY-MM-DD"
          saturdays.push(dateString);
        }
        date.setDate(date.getDate() + 1); // Move to the next day
      }
      return saturdays;
    }

    const allSaturdays2023 = getAllSaturdays(2023);
    
    jQuery("#"+this.questionId+" .InputText").flatpickr({
    dateFormat: "Y-m-d",
    minDate: new Date(),
    enable: allSaturdays2023        
    });

});


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 8 replies
  • May 11, 2023

Thank you! This works for me


Leave a Reply