Screen out based on embedded date variable and current month
Hi All,
I am stuck on a condition to screen out responses received after end of month from a embedded date variable. It should be dynamic as well.
Embedded DATE variable name: ShowDate
Example conditions below
I want to screen out anyone completes the survey with ShowDate of previous months.
If someone with show date 31/05/2023 completes the survey on 01/06/2023 then screen out.
The condition should work every month continuously.
If someone with show date 23/06/2023 completes the survey on 14/07/2023 then screen out.
OR
If someone with show date 15/02/2023 completes the survey on 25/07/2023 then screen out.
Only if the ShowDate month is the same as the current month should be made complete.
Could someone shed some light to solve this.
Thank you,
Praveen
Page 1 / 1
@praveengeorgeix
You need to compare the two dates via Custom Code and then change the flag. There is no out of the box comparison available.
Hope it helps!
Thanks Deepak. Im looking for the code.
Hi,
Qualtrics.SurveyEngine.addOnload(function() { /*Place your JavaScript here to run when the page loads*/ var tagShowDate = "${e://Field/tagShowDate}"; // Replace with the actual embedded data field name for TagShowDate var tagCurrentDate = "${e://Field/tagCurrentDate}"; // Replace with the actual embedded data field name for TagCurrentDate var dateBasedScreen = "${q://QID9/ChoiceGroup/SelectedChoices}"; // Replace with the actual question ID for DateBasedScreen
// Convert the date strings to Date objects var showDate = new Date(tagShowDate); var currentDate = new Date(tagCurrentDate);
// Get the last day of the month for the current date var lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0).getDate();
// Compare the showDate with the last day of the month if (showDate > new Date(currentDate.getFullYear(), currentDate.getMonth(), lastDayOfMonth)) { // Set the answer code to "Yes" if showDate is greater than the last day of the month dateBasedScreen = "Yes"; } else { // Set the answer code to "No" if showDate is not greater than the last day of the month dateBasedScreen = "No"; }
// Set the hidden question value Qualtrics.SurveyEngine.setEmbeddedData("dateBasedScreen", dateBasedScreen);
});
THis could work but something is making it not work. Could someone lok into this
@praveengeorgeix
If you update the below code date to replace your show date and add screenOut as embedded data in your survey flow you can then use it as branch logic to screen out.
Qualtrics.SurveyEngine.addOnload(function() { var showDate = "2023-06-11"; // Replace with the actual embedded data var showYear = showDate.substring(0, 4); var showMonth = showDate.substring(5, 7); var showDay = showDate.substring(8, 10); var showDateObj = new Date(showYear, showMonth - 1, showDay); var currentDate = new Date(); if (showDateObj.getMonth() !== currentDate.getMonth()) { // Screen out the response if the months are different Qualtrics.SurveyEngine.setEmbeddedData("screenOut", "true"); } else { Qualtrics.SurveyEngine.setEmbeddedData("screenOut", "false"); } });
Hope it helps!
@Deepak Im relatively new in Qualtrics. Could you please guide on where to paste this code? Is there a way to add this script in embedded data?
@praveengeorgeix You can include this code in surveys question only like any other JS just update the static date with your show date. Then you can use display logic of screen out to screen them out.
Thank you @Deepak I tried with as the below set up. but neither the embedded data (screenOut) or survey hidden variable (screenOut) seems to work. Could you please take a look.
Survey flow
Hidden question
JS inside hidden question
screen out is the embedded data in branch logic you have included it as question.
@Deepak Thank you. But if I delete that hidden question, where should I paste the JS? I know im being annoying, but Im stuck here. apologies Deepak
@You can include JS on the first question and based on that embedded data will be filled with show date and if it’s true screenout them on branch logic. You can include timing question if needed just for JS upload.
Its working @Deepak . You deserve a medal. Thank you very much.