A date question type is available that will give both a respondent interface for selecting a date and storing it in the response database as a datetime variable,
The methods and properties available on the form object returned from f("q1") when q1 is a date question, are described previously in chapters Conversion Methods in Forsta Plus, Methods of the Form Objects (go to Methods of the Form Objects for more information) and The f Function (go to About the f Function for more information). This part of the documentation provides some examples of using these methods.
Note: The Date question type is only available when using the Optimized Database format.
Set Current Date
If you would like to set current date in a hidden date question today, this can be done with the following script:
if(!f("today").toBoolean())
{
f("today").set(new Date());
}Validating that Date answered is not after Current Date
In this example we have a date question date1. We want to validate that the date is not after the current date. This can be done with the following script in the validation code field of the date question (date1).:
var dt = new Date(f("date1").year(),f("date1").month()-1,f("date1").day());
var current = new Date();
if(dt.valueOf() > current.valueOf())
{
RaiseError();
SetQuestionErrorMessage(LangIDs.en,"Please do not enter a date after the current date.");
}
Here we declare two instances of the Date object, and set one of them (dt) to the date answered, and the other (current) to the current date.
Converting Date of Birth into Age (in number of years)
If you have an date question dob where respondents insert their date of birth, you can calculate the respondent’s age and set it in a hidden question age with the following script, which is placed in the validation code of the question:
var birthday = new Date(f("dob").year(),f("dob").month()-1,f("dob").day());
var today = new Date();
var years = today.getFullYear() - birthday.getFullYear();
birthday.setYear(today.getFullYear());
// If your birthday hasn't occurred yet this year, subtract 1
if(today < birthday)
{
years-- ;
}
f("age").set(years);