help with choice display logic, please? | XM Community
Skip to main content
Question

help with choice display logic, please?

  • September 5, 2023
  • 8 replies
  • 168 views

hmacdermott
Level 2 ●●
Forum|alt.badge.img+4

I have a multiple choice question where the answer options are dates (i.e. 9/11/23, 10/2/23, etc.). Each answer option should only be visible for 60 days beyond the date (i.e. 11/11/23, 12/2/23, etc.)

 

In survey flow, I’ve set embedded data where CurrentDate = ${date://CurrentDate/SL}.
 

But I’m not sure how to set up the choice display logic.
 

Would it be “Display this Choice only if the following condition is met:
Embedded Data | Current Date | Is Less Than | ${date://CurrentDate/SL} + 60 days”??

 

Or can I input 11/11/23 instead??

Thanks for reading!

 

 

8 replies

Aanurag_QC
QPN Level 5 ●●●●●
Forum|alt.badge.img+31
  • 256 replies
  • September 5, 2023

Hello,

 

You would need to setup ED field with current date and use a JS on a question prior such that the the ED field automatically updates the values on your question choices. 

 

I hope I made sense :-)


hmacdermott
Level 2 ●●
Forum|alt.badge.img+4
  • Author
  • Level 2 ●●
  • 12 replies
  • September 5, 2023

Like this?

 

Qualtrics.SurveyEngine.addOnload(function()

{

    // Get the current date from embedded data

    var currentDateStr = "${e://Field/CurrentDate}";

    var currentDate = new Date(currentDateStr);

    

    // Define your date choices in the format [elementID, date]

    var choices = [

        ['q://QID1/1', new Date('2023-09-11')],

        ['q://QID1/2', new Date('2023-10-02')],

        // Add more choices here

    ];

    

    choices.forEach(function(choice) {

        var elementId = choice[0];

        var choiceDate = choice[1];

        

        // Calculate the date 60 days after the choice date

        var expiryDate = new Date(choiceDate);

        expiryDate.setDate(choiceDate.getDate() + 60);

        

        // If the current date is after the expiry date, hide the choice

        if (currentDate > expiryDate) {

            jQuery('#' + elementId).parent().hide()

;

        }

    });

});


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • QPN Level 8 ●●●●●●●●
  • 1090 replies
  • September 5, 2023

@hmacdermott Where do you get that code? ChatGPT? Trust me, it doesn’t work. It take more than that for a custom Javascript.


Try set your embedded data like this number format

Current Date = ${date://CurrentDate/Y}${date://CurrentDate/m}${date://CurrentDate/d}

Your current date will be save as number (i.e today is Current Date = 20230905)
In the option place Display this Choice only if the following condition is met:
Embedded Data | Current Date | Is Less Than | 20231111 (60 days beyond the date)

I’ve just answer a similar question like this, for more information, check out this topic 

 


hmacdermott
Level 2 ●●
Forum|alt.badge.img+4
  • Author
  • Level 2 ●●
  • 12 replies
  • September 5, 2023

Ah, I see. Thank you so much. I will try this!


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • QPN Level 8 ●●●●●●●●
  • 1090 replies
  • September 5, 2023

@hmacdermott Let me know if anything comes up


hmacdermott
Level 2 ●●
Forum|alt.badge.img+4
  • Author
  • Level 2 ●●
  • 12 replies
  • September 6, 2023

@dxconnamnguyen Thanks again!!! 

Current Date = ${date://CurrentDate/Y}${date://CurrentDate/m}${date://CurrentDate/d} was exactly what I needed and worked great.


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • QPN Level 8 ●●●●●●●●
  • 1090 replies
  • September 6, 2023

@hmacdermott Glad to know that 👍


Aanurag_QC
QPN Level 5 ●●●●●
Forum|alt.badge.img+31
  • 256 replies
  • September 6, 2023
hmacdermott wrote:

Like this?

 

Qualtrics.SurveyEngine.addOnload(function()

{

    // Get the current date from embedded data

    var currentDateStr = "${e://Field/CurrentDate}";

    var currentDate = new Date(currentDateStr);

    

    // Define your date choices in the format [elementID, date]

    var choices = [

        ['q://QID1/1', new Date('2023-09-11')],

        ['q://QID1/2', new Date('2023-10-02')],

        // Add more choices here

    ];

    

    choices.forEach(function(choice) {

        var elementId = choice[0];

        var choiceDate = choice[1];

        

        // Calculate the date 60 days after the choice date

        var expiryDate = new Date(choiceDate);

        expiryDate.setDate(choiceDate.getDate() + 60);

        

        // If the current date is after the expiry date, hide the choice

        if (currentDate > expiryDate) {

            jQuery('#' + elementId).parent().hide()

;

        }

    });

});

hmacdermott wrote:

Like this?

 

Qualtrics.SurveyEngine.addOnload(function()

{

    // Get the current date from embedded data

    var currentDateStr = "${e://Field/CurrentDate}";

    var currentDate = new Date(currentDateStr);

    

    // Define your date choices in the format [elementID, date]

    var choices = [

        ['q://QID1/1', new Date('2023-09-11')],

        ['q://QID1/2', new Date('2023-10-02')],

        // Add more choices here

    ];

    

    choices.forEach(function(choice) {

        var elementId = choice[0];

        var choiceDate = choice[1];

        

        // Calculate the date 60 days after the choice date

        var expiryDate = new Date(choiceDate);

        expiryDate.setDate(choiceDate.getDate() + 60);

        

        // If the current date is after the expiry date, hide the choice

        if (currentDate > expiryDate) {

            jQuery('#' + elementId).parent().hide()

;

        }

    });

});

That looks about right… I would need to test it out on a dummy survey to confirm. Thanks!


Leave a Reply