- Posts: 115
- Thank you received: 4
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Easiest way to capture Time
- dweisser
- Topic Author
- Offline
- Premium Member
Less
More
10 years 2 months ago #105236
by dweisser
Easiest way to capture Time was created by dweisser
I have a survey where people are going to enter their time of arrival am or pm.
I'm having a hard time find a solution that doesn't entail some additional programming.
Anyone have an idea?
David
I'm having a hard time find a solution that doesn't entail some additional programming.
Anyone have an idea?
David
The topic has been locked.
- dweisser
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 115
- Thank you received: 4
10 years 2 months ago #105238
by dweisser
Replied by dweisser on topic Easiest way to capture Time
Update:
I implemented this. Simple enough. Now to try and get the AM or PM in there.
If anyone is interested, I'll be happy to post a step-by-step.
David
I implemented this. Simple enough. Now to try and get the AM or PM in there.
If anyone is interested, I'll be happy to post a step-by-step.
David
The topic has been locked.
- byan
- Offline
- Junior Member
Less
More
- Posts: 39
- Thank you received: 11
10 years 2 months ago #105240
by byan
Replied by byan on topic Easiest way to capture Time
could you please share? Thanks!
The topic has been locked.
- dweisser
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 115
- Thank you received: 4
10 years 2 months ago - 10 years 2 months ago #105245
by dweisser
Replied by dweisser on topic Easiest way to capture Time
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:
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
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
Last edit: 10 years 2 months ago by dweisser.
The following user(s) said Thank You: byan
The topic has been locked.
- dweisser
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 115
- Thank you received: 4
10 years 2 months ago - 10 years 2 months ago #105261
by dweisser
Replied by dweisser on topic Easiest way to capture Time
The REGEX statement above is for 24-hour time and doesn't suit the purpose.
Technically, you don't need it as long as respondents cooperate and enter valid 12-hour time.
I changed it to this:
Seems to work ok.
David
Technically, you don't need it as long as respondents cooperate and enter valid 12-hour time.
I changed it to this:
Code:
regexMatch("/^(?:[1-9][0-2]|[1-9]):[0-5][0-9](?::[0-5][0-9])?$/",this)
Seems to work ok.
David
Last edit: 10 years 2 months ago by dweisser.
The following user(s) said Thank You: byan
The topic has been locked.
- mfaber
- Offline
- Junior Member
Less
More
- Posts: 22
- Thank you received: 6
10 years 2 months ago #105266
by mfaber
Replied by mfaber on topic Easiest way to capture Time
If you are using a recent 2.05 version, try adding a date/time question and enter HH:MM in the format field in the advanced attributes. Works with 24h-time format. Not sure about am/pm.
Screenshot for the timepicker at: manual.limesurvey.org/Question_type_-_Date
Screenshot for the timepicker at: manual.limesurvey.org/Question_type_-_Date
The following user(s) said Thank You: byan
The topic has been locked.
- mfaber
- Offline
- Junior Member
Less
More
- Posts: 22
- Thank you received: 6
10 years 2 months ago #105267
by mfaber
Replied by mfaber on topic Easiest way to capture Time
Calculations SHOULD also work... try using {departuretime-arrivaltime} or whatever your question codes are named.
The following user(s) said Thank You: byan
The topic has been locked.