# How to Calculate the Difference Between Time Stamps

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) * 60 + parseInt(_split_time_1);
var _minutes_2 = parseInt(_split_time_2) * 60 + parseInt(_split_time_2);

var _minutes_diff = _minutes_2 - _minutes_1;

_minutes_diff```

### 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;
}

_minutes_diff```

## 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;
_hour_diff```

## 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 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 seconds.
7. Click OK.
`Q.SecondDif(TIME1, TIME2)`

## 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```