Computing Hours From Time Stamps
Jump to navigation
Jump to search
Pages with syntax highlighting errors
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > JavaScript
User Interface > JavaScript Variables > JavaScript Variables Examples Library
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
- Create a new JavaScript Variable.
- Use the Expression of Q.Hour(date_variable), where date_variable is the name of your date variable.
- Press OK.
Pages with syntax highlighting errors
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > JavaScript
User Interface > JavaScript Variables > JavaScript Variables Examples Library