I used two short text questions to get Time Entered and Time left. And a third short text question to capture the TimeBetween.
First, I have to make sure they enter the times in a specific format so I can subtract Time Entered from Time Left.
Add the following regular expression to each TimeEntered and TimeLeft question's validation tip:
Code:
regexMatch("/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/",this)
This will ensure the respondent enters only valid times (01:15, 12:45) no invliad times (13:45, 22Lfty, etc.) DOES ANYONE KNOW HOW TO EDIT THE REGEX TO ALLOW THE LEADING ZEROES TO BE DROPPED?
Next, I put some javascript in template.js to do the math for TimeBetween. There's a much more eloquent way to write this, but I don;t know what it is.
Be sure to change: #answer944284X236X1689 to your SurveyXGroupXQuestion identifiers. The code uses the answers to the first two questions in order to calculate the the third. It assign the calculation's result to the third items answer using an onchange event on the second answer. I made mine required to ensure that the event would be triggered.
$(document).ready(function(){
// Check if the answer is on this page...
$('#answer944284X236X1689').change(function(event) {
if($('#answer944284X236X1688').length > 0 && $('#answer944284X236X1689').length > 0) {
var StartTime = $('#answer944284X236X1688').val();
var EndTime = $('#answer944284X236X1689').val();
var s = StartTime.split(':');
if (s[0] > 6 && s[0] <= 12) {
var s= parseInt(s[0]) * 60 + parseInt(s[1]);
}
else {
var s= parseInt(s[0]) * 60 + parseInt(s[1]) + (12*60);
}
var e = EndTime.split(':');
if (e[0] > 6 && e[0] <= 12) {
var e= parseInt(e[0]) * 60 + parseInt(e[1]);
}
else {
var e= parseInt(e[0]) * 60 + parseInt(e[1]) + (12*60);
}
var TimeElapsed = e - s;
if(isNaN(TimeElapsed)){
document.getElementById('answer944284X236X1690').value= '';
}
else{
document.getElementById('answer944284X236X1690').value= TimeElapsed;
}
//alert(TimeElapsed);
}
})
});
I hope this is helpful.
David