Generating dates 30 days into the past on the basis of a user-entered value | XM Community
Skip to main content

Hello,

I am working on a project and I could use some help. Right now, I have something called a timeline follow back where participants report any substance use they’ve had in the past 30 days. In my current Qualtrics project, these dates are populated based on the date that the user takes the survey and uses embedded data to do this.

However, I have some surveys that did not get entered on the day they were conducted (they’re conducted on paper and entered into Qualtrics on the same day, generally). I need to set up a Qualtrics project in which the user can pick the date that the timeline follow back was completed, and then have the 30 days before that day populated so that the retroactive data can be entered. 

I’ve tried editing the embedded data so that the date data is piped in from a question in which the user chooses the date, but that did not work. I’ve tried to alter the embedded data a few different ways but I can’t seem to figure it out. 

I am hoping someone here can help me figure out how I can have a user choose the day that the original timeline follow back was completed, and use that ‘completed date’ to populate the dates 30 days before the ‘completed date’. 

I believe I may need to use JavaScript for this, but unfortunately I’m not well versed enough to do this yet. Any help would be greatly appreciated!

@dwags Place this code in your header source:
<script src="https://cdn.jsdelivr.net/npm/luxon@3.4/build/global/luxon.min.js"></script>

In your text entry add this JS:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;

var inputDate = DateTime.fromFormat(jQuery("#" + this.questionId + " .InputText").val(), "dd/MM/yyyy"); // The format is dd/MM/yyyy

var thirtyDaysBefore = inputDate.minus({ days: 30 });
var formattedDate = thirtyDaysBefore.toFormat("dd/MM/yyyy");
Qualtrics.SurveyEngine.setEmbeddedData('30DaysBefore', formattedDate);
});

Piped-text the result in the next question with: ${e://Field/30DaysBefore}. Make sure there’s page brake between them. If you want to save the ‘30daysbefore’ embedded data for report, declare it in the survey flow.
Hope it helps


Leave a Reply