Welcome, Guest
Username: Password: Remember me

TOPIC: Easiest way to capture Time

Easiest way to capture Time 8 months 6 days ago #105236

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Bronze Donor
  • Posts: 205
  • Thank you received: 5
  • Karma: 5
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
The administrator has disabled public write access.

Easiest way to capture Time 8 months 6 days ago #105238

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Bronze Donor
  • Posts: 205
  • Thank you received: 5
  • Karma: 5
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
The administrator has disabled public write access.

Easiest way to capture Time 8 months 6 days ago #105240

  • boyan
  • boyan's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 49
  • Thank you received: 9
  • Karma: 1
could you please share? Thanks!
The administrator has disabled public write access.

Easiest way to capture Time 8 months 6 days ago #105245

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Bronze Donor
  • Posts: 205
  • Thank you received: 5
  • Karma: 5
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:
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: 8 months 6 days ago by dweisser.
The administrator has disabled public write access.
The following user(s) said Thank You: boyan

Easiest way to capture Time 8 months 6 days ago #105261

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Bronze Donor
  • Posts: 205
  • Thank you received: 5
  • Karma: 5
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. :P

I changed it to this:
regexMatch("/^(?:[1-9][0-2]|[1-9]):[0-5][0-9](?::[0-5][0-9])?$/",this)

Seems to work ok.
David
Last Edit: 8 months 6 days ago by dweisser.
The administrator has disabled public write access.
The following user(s) said Thank You: boyan

Easiest way to capture Time 8 months 5 days ago #105266

  • mfaber
  • mfaber's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 85
  • Thank you received: 22
  • Karma: 4
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
The administrator has disabled public write access.
The following user(s) said Thank You: boyan

Easiest way to capture Time 8 months 5 days ago #105267

  • mfaber
  • mfaber's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 85
  • Thank you received: 22
  • Karma: 4
Calculations SHOULD also work... try using {departuretime-arrivaltime} or whatever your question codes are named.
The administrator has disabled public write access.
The following user(s) said Thank You: boyan
Moderators: ITEd
Time to create page: 0.214 seconds
Donation Image