Computing Hours From Time Stamps

From Q
Jump to: navigation, search

Some data sets may contain time stamps in the format hh:mm:ss AM(PM), and it may be necessary to band these times together based on 24-hour time. For example, the analysis may require you to group respondents into 4-hour categories throughout the day. Q does not have a concept of time in the same sense as it has the concept of Date.

Where you have a text variable

When given a Text Variable (which we assume has the Variable Name TIME) containing time values in the format hh:mm:ss AM(PM), the following code will convert the values into values 1-24, which facilitates grouping of the values:

var _time_value = TIME;
var _split_values = _time_value.split(":");
var _hour = parseInt(_split_values[0]);
if (_time_value.indexOf("PM") > -1)
    _hour = _hour + 12
_hour

Change the Question Type of the resulting question to Pick One. This will allow you to group the values in the table.

Where you have a Date variable

  1. Create a new JavaScript Variable.
  2. Use the Expression of Q.Hour(date_variable), where date_variable is the name of your date variable.
  3. Press OK.