TLFB Dates with Java Script | XM Community
Skip to main content

Hello,

My name is Carrie and I’m fairly new to Java Script.  I used the below script in order to calculate and pipe in the last Sunday (past 7 days) for a timeline follow back that participants will be completing on Sundays.  This worked great since the survey is always calculating 7 days from the most recent Sunday. However, for a new survey I’m building, I want the script to define the past 7th day from whenever the participant first opens the survey.  Participants will be completing this survey from home, so the day will vary depending on when they start the survey.  I also need the past 7th date to remain the same regardless if they open the link on a Monday and then again on Tuesday to complete.  I tried the generic qualtrics piping but, that date recalculates using the current day, which means the dates change every time the participant clicks on the link. Is there a way to revise the code for what I’m needing?  Thank you!

 

                var d = new Date();

                var lastSun = new Date();

                lastSun.setDate(lastSun.getDate()-7-d.getDay());

                var dd = lastSun.getDate();

                var mm = lastSun.getMonth()+1;

                var yyyy = lastSun.getFullYear();

                if(dd<10) {

                                dd='0'+dd;

                }

                if(mm<10) {

                                mm='0'+mm;

                    }

                lastSunNew = mm+'/'+dd;

                Qualtrics.SurveyEngine.setEmbeddedData("1",lastSunNew);

 

 

@cmurphy20,

Save the start date into an embedded data field in ISO format at the beginning of you survey flow.

startdate = ${date://CurrentDate/c}

Then pipe the start date into your script:

var d = new Date("${e://Field/startdate}");

 


While storing in embedded data check first if the embedded data value is empty or filled and then start calculation.


  I named the embedded data feild “SDate”  So this is what the script should look like?             

 

                 var d = new Date("${e://Field/SDate}");

                var lastSun = new Date();

                lastSun.setDate(lastSun.getDate()-7-d.getDay());

                var dd = lastSun.getDate();

                var mm = lastSun.getMonth()+1;

                var yyyy = lastSun.getFullYear();

                if(dd<10) {

                                dd='0'+dd;

                }

                if(mm<10) {

                                mm='0'+mm;

                    }

                lastSunNew = mm+'/'+dd;

                Qualtrics.SurveyEngine.setEmbeddedData("1",lastSunNew);


  I named the embedded data feild “SDate”  So this is what the script should look like?             

 

                 var d = new Date("${e://Field/SDate}");

                var lastSun = new Date();

                lastSun.setDate(lastSun.getDate()-7-d.getDay());

                var dd = lastSun.getDate();

                var mm = lastSun.getMonth()+1;

                var yyyy = lastSun.getFullYear();

                if(dd<10) {

                                dd='0'+dd;

                }

                if(mm<10) {

                                mm='0'+mm;

                    }

                lastSunNew = mm+'/'+dd;

                Qualtrics.SurveyEngine.setEmbeddedData("1",lastSunNew);

Yes


I very much appreciate your help.  I have no clue if I’m adapting this correctly!  It’s still giving me the date of Sunday the 14th.  What else do i need to do to have it pull the date from seven days ago...So if they opened the link today, they would see the date January 17th. 


I very much appreciate your help.  I have no clue if I’m adapting this correctly!  It’s still giving me the date of Sunday the 14th.  What else do i need to do to have it pull the date from seven days ago...So if they opened the link today, they would see the date January 17th. 

I think your third line should be:

lastSun.setDate(d.getDate() - 7);

 


Tom, this worked great!  Thank you so much!  One last question….is it possible to do the same thing with the day of the week?  If so, how would I adapt the script for that?

 

Carrie


Tom, this worked great!  Thank you so much!  One last question….is it possible to do the same thing with the day of the week?  If so, how would I adapt the script for that?

 

Carrie

 You can do this:

var weekdays = d"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var dayOfWeek = weekdayselastSun.getDay()];

 


Leave a Reply