Sometimes you may want to create a custom date variable that measures the difference between two dates, such as an Age variable.
Method - Measure time since last date in data
Method - Measure time since hardcoded date
Method - Measure time between two date variables
Method - Compute Age from a Date of Birth
Technical details
The software has a built-in library of JavaScript functions that you can use in a JavaScript variable to do this automatically for each respondent. You can select what time interval you want to calculate the difference at by changing the Q.DayDif function to the appropriate one from the library.
Method - Measure time since last date in data
This script requires that the Access All Data Rows feature is checked. This enables the variable to look across all the cases in the data to find the most recent date in your date variable of choice (must be structured as a Date/Time variable). Then it will find the number of days (or a different time interval) from each date till this most recent one.
//Replace CreatedOn_date with the name of your Date/Time variable
var date_var = CreatedOn_date; // finding highest date var highest_date = 0; for (var i = 0; i < N; i++) { var cur_date = date_var[i]; if(cur_date > highest_date) highest_date = cur_date; }
// Computing the difference var result = new Array(N); //declaring an array to hold the results for (var i = 0; i < N; i++)
//you can change Q.DayDif to a different time interval from the library like Q.MonthDif etc result[i] = Q.DayDif(date_var[i], highest_date); result
You can change the above code to find the difference in months. For example, observations in the same calendar month are assigned a score of 0, those from the previous calendar month are assigned a value of 1, and so on. You will change the Q.DayDif function in the code above to Q.MonthDif like below:
result[i] = Q.MonthDif(date_var[i], highest_date);
Method - Measure time since hardcoded date
In the code below, you set a specified date to use to make difference. It currently finds the difference in the variable since April 30th, 2020.
//Replace CreatedOn_date with the name of your Date/Time variable
var date_var = CreatedOn_date; //modify the date to measure from like Q.EncodeDate(y, m, d)
var since_date = Q.Encode(2020, 04, 30); //you can change Q.DayDif to a different time interval from the library like Q.MonthDif etc Q.DayDif(since_date, date_var);
Method - Measure time between two date variables
In the code below, you can find the difference between two Date/Time variables.
//Replace CreatedOn_date with the name of your Date/Time variable
var begin_date = CreatedOn_date; //Replace SolvedOn_date with the name of your second Date/Time variable
var end_date = SolvedOn_date;
//you can change Q.DayDif to a different time interval from the library like Q.MonthDif etc
Q.DayDif(begin_date, end_date);
Method - Compute Age from a Date of Birth
Sometimes information about the respondents' ages are collected indirectly by asking for their date of birth. If this information is stored in a Date variable, then the following code can be used to compute the respondents' age in years. This code requires you to specify a reference date (e.g. today's date) in order to calculate the age. Please change the date specified in the first line before using this code. This code refers to a Date variable called date. Please replace this with the appropriate Variable Name for your Date of Birth variable.
var reference_dat = Q.AsDate('17/04/2013'); var dob = date; var age_in_years = NaN; if (Q.Month(dob) < Q.Month(reference_dat)){ age_in_years = Q.Year(reference_dat) - Q.Year(dob); } else if (Q.Month(dob) == Q.Month(reference_dat) && Q.Day(reference_dat) >= Q.Day(dob)) { age_in_years = Q.Year(reference_dat) - Q.Year(dob); } else age_in_years = Q.Year(reference_dat) - Q.Year(dob) - 1; age_in_years;