How to Calculate the Difference Between Time Stamps

From Q
Jump to: navigation, search

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 time stamps, 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 time stamps of the format hh:mm (which stands for hours and minutes) in 24-hour time. There are other variations in time stamp formats which are not covered in this article.

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 which 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 Minutes

To calculate the number of minutes between the two time stamps you can:

  1. Go to the Variables and Questions tab.
  2. Right-click and select Insert Variables > JavaScript Formula > Numeric.
  3. Paste the formula below into the Expression.
  4. Change TIME1 and TIME2 to the Names of the variables in your own data.
  5. Check the entries in the Result column to ensure that they make sense.
  6. Enter the Label: Time difference in minutes.
  7. Click OK.
var _time_1 = TIME1;
var _time_2 = TIME2;

var _split_time_1 = _time_1.split(":");
var _split_time_2 = _time_2.split(":");

var _minutes_1 = parseInt(_split_time_1[0]) * 60 + parseInt(_split_time_1[1]);
var _minutes_2 = parseInt(_split_time_2[0]) * 60 + parseInt(_split_time_2[1]);

var _minutes_diff = _minutes_2 - _minutes_1;


Values are negative

If you find negative values in the Result and you expect the difference to always be positive then it is likely that some time-stamps have crossed over into the next day. To correct this, remove the last line of the formula above and instead add:

if (_minutes_diff < 0) {
    _minutes_diff = _minutes_diff + 24*60;    


Calculating the Difference in Hours

To calculate the same difference in terms of the number of hours, first create the variable above in minutes, and then:

  1. Right-click the row for the variable and select Edit Variable.
  2. Remove the last line of the formula and replace it with the expression below.
  3. Change the Label to: Time Difference in Hours.
  4. Click OK.
var _hour_diff = _minutes_diff / 60;

Showing the Difference as Hours:Minutes

To create a new variable which shows the number of hours and minutes you can:

  1. Create the variable above for the difference in minutes.
  2. Right-click in the Variables and Questions tab and select Insert Variables > JavaScript Formula > Text.
  3. Paste the formula below into the Expression.
  4. Replace MINUTE_DIFF with the Variable Name of the variable which gives the difference in minutes.
  5. Change the Label to: Difference in hours and minutes.
  6. Click OK.
  7. 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)