Date and Time Calculations

Though JScript.NET can be used in date object calculations, date and time calculations can be tricky. Calculations that use the current time must take into account the fact that Illume re-calculates calculated variables every time participants move from one page to the next.

Date calculations are tricky because programmers often make incorrect assumptions about units of time. For example, because the JScript date object uses milliseconds as its unit of time, you might think it’s easy to measure a span of years by converting years to milliseconds and performing simple arithmetic on the milliseconds. This may lead to incorrect calculations, since leap years are longer than standard years.

Months are notoriously irregular. They can have 30 days, or 31, or 28, or in some cases, 29.  Days irregular as well. In most parts of the US, the first Sunday in April is a 23-hour day, and the last Sunday in October is a 25-hour day.

Below are some examples utilizing Dates.  The following JScript calculates a participant’s age on the date of the survey.   This calculation assumes that the variable BIRTHDATE is a variable in the survey that asks for the participant’s date of birth, and that variable is of data type Date:

var today = new Date();

var dob = new Date({Value:BIRTHDATE});

var yearDiff = today.getFullYear() – dob.getFullYear(); var monthDiff = today.getMonth() – dob.getMonth();

var dayDiff = today.getDate() – dob.getDate();

if(monthDiff < 0 || (monthDiff == 0 && dayDiff < 0))




If BIRTHDATE were a text variable, not a Date data type variable, and had the format mm/dd/yyyy, the calculation would be as follows:

var today = new Date();

var dob = {Response:BIRTHDATE} + “”;

var dateParts = dob.split(/\D/);

var month = parseInt(dateParts[0], 10);

var day = parseInt(dateParts[1], 10);

var year = parseInt(dateParts[2], 10);

if(year < 100)

 year += 1900;

var yearDiff = today.getFullYear() – year;

var monthDiff = today.getMonth() – ( month – 1);

var dayDiff = today.getDate() – day;

if(monthDiff < 0 || (monthDiff == 0 && dayDiff < 0))




NOTE: The value of the value of the calculated variable is set to the value of the last expression evaluated in the JScript. In the examples above, that is the variable yeardiff. Keep in mind that because Illume uses eval() to evaluate the JScript in calculated variables, a return at the end of the script is not needed.