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 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.

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

  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 Date1 and Date2 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.
Q.HourDif(Date1, Date2)


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 Date1 and Date2 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.
Q.MinuteDif(Date1, Date2)


Calculating the Difference in Seconds

  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 Date1 and Date2 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 seconds.
  7. 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:

  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)
_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:

  1. Create the variable above for the difference in seconds.
  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 SECOND_DIFF with the Variable Name of the variable which gives the difference in seconds.
  5. Change the Label to: Difference in hours, minutes and seconds.
  6. Click OK.
  7. 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