Respondents record a start time (time to bed) and and end time (time out of bed) in separate questions using flatpickr’s time picker. The responses are recorded in the format, for example, 11:00 PM and 8:00 AM, for start time and end time respectively.
I need to calculate time in bed = (time out of bed - time to bed) in minutes.
To calculate time in bed, I attempted the following:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function()
{
// get the values of the start and end time questions
var timeToBed = "${q://QID4/ChoiceTextEntryValue}";
var timeOutOfBed = "${q://QID12/ChoiceTextEntryValue}";
// create Date objects for the start and end times
var timeToBedDate = new Date("1/1/2000 " + timeToBed);
var timeOutOfBedDate = new Date("1/1/2000 " + timeOutOfBed);
// check if the end time is before the start time (i.e. the times are on different days)
if (timeOutOfBed < timeToBed) {
timeOutOfBedDate.setDate(timeOutOfBedDate.getDate() + 1);
}
// calculate the difference in minutes
var timeInBedMinutes = (timeOutOfBedDate - timeToBedDate) / 1000 / 60;
// save the difference as an embedded data variable
Qualtrics.SurveyEngine.setEmbeddedData("timeInBed", timeInBedMinutes);
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
Via Active questions tagged javascript - Stack Overflow https://ift.tt/R5uPKkB
Comments
Post a Comment