Introduction
Some data sets contain information about the time-of-day of events related to the questionnaire, for example, the time the respondent's flight arrived or departed, without having complete information about the date (i.e. information about the day, month, or year). Q has inbuilt functionality to handle Dates of the standard formats, but these do not apply to timestamps alone. While complete date variables will usually be shown as Date questions in Q, timestamps will usually appear as Text questions. If you need to do calculations with timestamps, then some manipulation with JavaScript is required.
The sections below describe methods that can be used to compute the time difference between two variables which contain timestamps of the format hh:mm (which stands for hours and minutes) in 24-hour time. There are other variations in timestamp formats that are not covered in this article.
Method
When creating the JavaScript variables you should check the Result column in the Preview of results section to inspect the final values for each respondent to make sure that the values make sense for your data. For example, if you obtain values that are negative but you expect only positive results because the second time is always later than the first then you need to correct for this (an example of this is shown below).
Calculating the Difference in Hours
- Go to the Variables and Questions tab.
- Right-click and select Insert Variables > JavaScript Formula > Numeric.
- Paste the formula below into the Expression.
- Change Date1 and Date2 to the Names of the variables in your own data.
- Check the entries in the Result column to ensure that they make sense.
- Enter the Label: Time difference in minutes.
- Click OK.
Q.HourDif(Date1, Date2)
Calculating the Difference in Minutes
To calculate the number of minutes between the two time stamps you can:
- Go to the Variables and Questions tab.
- Right-click and select Insert Variables > JavaScript Formula > Numeric.
- Paste the formula below into the Expression.
- Change Date1 and Date2 to the Names of the variables in your own data.
- Check the entries in the Result column to ensure that they make sense.
- Enter the Label: Time difference in minutes.
- Click OK.
Q.MinuteDif(Date1, Date2)
Calculating the Difference in Seconds
- Go to the Variables and Questions tab.
- Right-click and select Insert Variables > JavaScript Formula > Numeric.
- Paste the formula below into the Expression.
- Change Date1 and Date2 to the Names of the variables in your own data.
- Check the entries in the Result column to ensure that they make sense.
- Enter the Label: Time difference in seconds.
- Click OK.
Q.SecondDif(Date1, Date2)
Showing the Difference as Hours:Minutes
To create a new variable which shows the number of hours and minutes you can:
- Create the variable above for the difference in minutes.
- Right-click in the Variables and Questions tab and select Insert Variables > JavaScript Formula > Text.
- Paste the formula below into the Expression.
- Replace MINUTE_DIFF with the Variable Name of the variable which gives the difference in minutes.
- Change the Label to: Difference in hours and minutes.
- Click OK.
- To use the new values as categories, change the Variable Type of the new variable to Categorical.
var _minutes_diff = MINUTE_DIFF;
var _positive_minutes_diff = Math.abs(_minutes_diff);
var _sign = _minutes_diff < 0 ? -1 : 1;
var _hours = Math.floor(_positive_minutes_diff/60);
var _minutes = _positive_minutes_diff % 60;
var _new_stamp = (_sign < 0 ? "-" : "") + (_hours < 10 ? "0" + _hours : _hours) + ":" + (_minutes < 10 ? "0" + _minutes : _minutes)
_new_stamp
Showing the Difference as Hours:Minutes:Seconds
To create a new variable which shows the number of hours, minutes and seconds you can:
- Create the variable above for the difference in seconds.
- Right-click in the Variables and Questions tab and select Insert Variables > JavaScript Formula > Text.
- Paste the formula below into the Expression.
- Replace SECOND_DIFF with the Variable Name of the variable which gives the difference in seconds.
- Change the Label to: Difference in hours, minutes and seconds.
- Click OK.
- To use the new values as categories, change the Variable Type of the new variable to Categorical.
var _sec_diff = SECOND_DIFF;
var _positive_sec_diff = Math.abs(_sec_diff);
var _sign = _sec_diff < 0 ? -1 : 1;
var _hours = Math.floor(_positive_sec_diff/3600);
var _minutes = _positive_sec_diff % 3600;
var _minutes1 = Math.floor(_minutes/60);
var _seconds = _minutes1 % 60;
var _new_stamp = (_signH < 0 ? "-" : "") + (_hours < 10 ? "0" + _hours : _hours) + ":" + (_minutes1 < 10 ? "0" + _minutes1 : _minutes1) + ":" + (_seconds < 10 ? "0" + _seconds : _seconds)
_new_stamp
Next
How to Convert Dates into Different Time Zones in Q
How to Set Time Periods for Date Questions
How to Create a Time Series Graph