Solved
Generating dates into the future on the basis of a user-entered value
I am creating a survey which requires users to specify the start date of a programme on the first page, then populate 4 matrix tables with a value for each of the following 28 days. I am trying to make the scale points on each table dates generated on the basis of the user's original entry value (+1, +2... +28). Not sure how to get qualtrics to recognise the original entry as a date (rather than just text) so I can perform this addition. I have come across a problem of a somewhat similar nature in the support pages where the person asking the question was directed toward 'moment.js' but details were sparse and I couldn't quite get my head round how to integrate this. I'd be grateful for any help or suggestions.
Best answer by fleb
Hi @sumwonels,
there are more ways how to do this. Here is one of them:
1) Create an HTML element with an ID in each label of a row in your matrix. Start with this one `<div id = "d0">0</div>` and then increase both numbers by one in each following line.
2) Compute the last date and then get all dates between the start date and the end date using this function from GitHub.
3) Convert your dates into the correct format and put these values into your HTML elements.
So the whole code could look somehow like this:
Qualtrics.SurveyEngine.addOnload(function()
{
function stringToDate(_date,_format,_delimiter)
{
var formatLowerCase=_format.toLowerCase();
var formatItems=formatLowerCase.split(_delimiter);
var dateItems=_date.split(_delimiter);
var monthIndex=formatItems.indexOf("mm");
var dayIndex=formatItems.indexOf("dd");
var yearIndex=formatItems.indexOf("yyyy");
var month=parseInt(dateItems[monthIndex]);
month-=1;
var formatedDate = new Date(dateItems[yearIndex],month,dateItems[dayIndex]);
return formatedDate;
}
var getDates = function(startDate, endDate) {
var dates = [],
currentDate = startDate,
addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
var start = stringToDate("${q://QID10/ChoiceTextEntryValue}","dd/MM/yyyy","/");
var end = new Date();
end = end.setDate(start.getDate() + 2);
var dates = getDates(start, end);
var i;
var final_date;
for (i = 0; i < dates.length; i++)
{//temp = dates[i].getUTCDay() + ". " + (dates[i].getUTCMonth() + 1) +". "+ dates[i].getUTCFullYear();
//temp = dates[i][2]+'/'+ dates[i][1]+'/'+ dates[i][0];
var final_date = dates[i].getDate()+". "+(dates[i].getMonth()+1)+". "+dates[i].getFullYear();
document.getElementById("d"+i).innerHTML = final_date;}
});
Note: Don't forget to change the ID of the question to yours and the end date to correct one.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
