18  Date Formatting

In Qualtrics, there are multiple ways to ask respondents to enter a date. Some perform much better than others, but none are thoroughly explained in the official Qualtrics documentation. We offer various suggestions below that are relatively easy to implement and will export usable data.

18.1 Text Entry Question with Validation (Easiest)

The simplest way to obtain a date is for respondents to type it in.

As outlined in the image below, you can create a text entry question, then add validation by content type, then select the appropriate “Date format” option (e.g., mm/dd/yyyy, dd/mm/yyyy, or yyyy/mm/dd).

Steps to add content type validation

This method lets respondents enter any valid date between 01/01/1000 and 12/31/2999. It requires leading zeroes (e.g., 01/01/2020 instead of 1/1/2020) and prevents typing invalid dates (e.g., February 30th). It allows “/”, “-”, or “.” as the separator, but most respondents will use “/” if it is suggested in the text of the question.

18.2 Text Entry Question with JavaScript

After creating a text entry question, you can add a JavaScript element that will provide the “mm/dd/yyyy” template as well as a clickable date picker.

Date entry question as it will appear to respondents. It says enter a date and provides a mm/dd/yyyy suggestion in the answer space with a clickable calendar icon to the right

When clicked, the date picker displays a small popup calendar.

Survey question with a small popup calendar

To add the JavaScript, select the question, and in the “Edit Question” pane, scroll all the way down to the “Question Behavior” section and click “JavaScript.”

Javascript option in the question behavior menu

The pop-up window will contain 3 small chunks of code. Replace the first chunk of code with the following:

Qualtrics.SurveyEngine.addOnload(function() {
    var textInputs = this.questionContainer.querySelectorAll('input[type="text"]');
    if (typeof textInputs[0] !== 'undefined') {
        textInputs[0].type = 'date';
    }
});

Javascript pop-up window after adding the custom code chunk

Then click “Save.”

JavaScript code comes from Curt Grimes on StackOverflow

Note on validation

Adding the JavaScript does not validate dates, so respondents could choose to enter “01/01/0001” or “02/30/111111”. If you anticipate this being a problem with your respondents, you can add the validation described above in addition to the Javascript, but if you do so, you must change the date format to “yyyy/mm/dd”. This is because the JavaScript converts all dates to the “yyyy-mm-dd” format behind the scenes, so respondents will not be able to answer the question or finish the survey if another validation format is selected. Moreover, once the correct options are selected, it can still be confusing for a respondent who types “12/31/2015” and sees the error message, “Please enter a valid date of the form yyyy/mm/dd”, though this should seldom occur in normal usage.

18.3 Text Entry Question with HTML

There are two ways to create a question with HTML code dates:

  1. Follow the instructions under Qualtrics Question Templates below to insert a preformatted question.
  2. Alternatively, create a text entry question, click “HTML View,” and copy-paste this code from the Brown University OIT website.

In our testing, the question will include a clickable calendar, but the calendar will break if you try to edit the question text outside of HTML View.

Survey question with a calendar above the response area. Text says enter a date

This is because the calendar is stored in the HTML code, which is only visible by clicking the question text and choosing “HTML View.” In HTML view, you can edit the question text (highlighted yellow below), but if you edit the question text in the normal view, it will overwrite all the HTML code and remove the calendar.

Question editor after clicking HTML view This method will auto-populate the text entry box with a date in the format “dd-mm-yyyy”, so it can be used with the content validation options described above if you so choose.

18.4 Qualtrics Question Templates (results may vary)

Qualtrics includes 3 pre-formatted date questions, which can be accessed by choosing “Import from Library” in the survey builder.

Screenshot highlighting the import from library button

In the pop-up window, choose “Demographics” under the Qualtrics Library.

Demographics section of the import menu

Then choose “Calendar & Date Questions.”

Calendar and Date Questions in the import menu

Choose one of the three options.

Three premade date question options

The Side-by-side and Matrix date question templates default to including all years from 1900 to 2049 in their drop-down menus, though this can be configured by entering the Javascript editor. Unlike the clickable calendar, you can edit the question text of these question templates without losing the functionality.

The default Side-by-side and matrix date picker questions

We do not recommend the Side-by-side or Matrix templates, however, because in the dataset they will place month, day, and year into separate columns, which hinders efforts to sort or graph results by date. These columns cannot be combined without exporting the survey data.

Screenshot showing survey data where the month, day, and year are in separate columns.

Important

If you choose the “Calendar Questions” block instead of “Calendar & Date Questions,” there will be a fourth option, which is a drill-down question. The drill down question is not recommended because it does not export well. For example, in SPSS, months are labelled correctly but the values show no discernible pattern, e.g. “1 = January, 2389 = February, 4623 = March” etc.

18.5 Exporting Date Data

18.5.1 Data Types

A Text Entry Question with Validation will record any valid date between 01/01/1000 and 12/31/2999 that uses “/”, “-”, or “.” as the separator, and can be configured to specify mm/dd/yyyy, dd/mm/yyyy, or yyyy/mm/dd format.

A question using the suggested JavaScript code will record dates in the format “yyyy-mm-dd”.

Qualtrics’ suggested Matrix and Side-by-Side questions will record month, day, and year in separate columns.

18.5.2 Excel

Dates using “/” or “-” as the separator will export correctly to Excel, and dates after Jan 1, 1900 will be automatically recognized by Excel as dates.

18.5.3 SPSS

Dates will export as a string variable to SPSS, but can be easily fixed by opening a syntax editor and calling alter type VARNAME (adate10). (This command will automatically interpret the separators “/”, “-”, and “.”)

Note: If the data is in mm/dd/yyyy format, use adate10. If it is in yyyy/mm/dd format, use sdate10, and if it is in dd/mm/yyyy format, use edate10.

18.5.4 R

In R, you can import the dates normally as a .csv file, though importing a .xlsx or .sav file may be easier. If you choose to use .csv and find that respondents used different separators, they can be converted to a single separator using stringr::str_replace_all(VARNAME, '[:punct:]', '/') which will replace all punctuation in the variable with “/”, which can then be parsed into a date using as.Date() or readr::parse_date().