Combine month and year values into a single date

Updated at September 21st, 2020

Protobi can help with converting date values into a desired format for analysis. In our Date/time values tutorial, we demonstrated a few basic ways to parse and analyze dates. This tutorial explains how to programmatically combine separate month and year values into date strings. 


Surveys may elicit dates, not as dates, but as separate questions (e.g. year, month, day). We can use data process to convert these into date strings that Protobi can recognize. 

Consider a question that asked for month and year separately: 

Note: The month values in q1_2 are actually numeric, and month names are the format.

Data processing code

The code starts with a simple return rows, which just returns an array containing your dataset. The program iterates over this array and creates a new data column for row.q1 that concatenates the month and year values into strings.

We used an existing key "row.q1" as the element that will contain the string values. However, you can make up a new key which will write a new element containing the date strings into existence. 

Paste the code into the process pre-calculate, and make any necessary changes. Press run, and save.

var rows = data.main;

rows.forEach( function(row) { 
       row.q1 = row.q1_1 + '-' + row.q1_2.padStart(2, '0') + '-01'+ 'T12:00:00' // create YYYY-MM-DD-T formatted string

return rows;

For projects with many dates to convert, the code below may be easier to use and keep organized.

Alternative code option

For the date fields enter:

  • key: Parent group's key can be used or use a new key 
  • month: Month element's key
  • year: Year element's key
var rows = data.main;

// Convert Month/Year pairs to date strings
var date_fields = [
  {key:  "q1",   month: "q1_1", year: "q1_2"}, // combine q1 month and year
  {key:  "q2",   month: "q2_1", year: "q2_2"}, // combine q2 month and year
  {key:  "q3",   month: "q3_1", year: "q3_2"}, // combine q3 month and year


    _.each(date_fields, function(entry) {
        if (row[entry.year]) {
            var date_str = row[entry.year] + '-' + row[entry.month].padStart(2, '0') + '-01' + 'T12:00:00'
            row[entry.key] = date_str//moment(date_str, 'YYYY-MM')

return rows

Resulting date strings

Open your project. If it's already open, refresh the page. Keys that didn't previously exist, but were written into existence in the code should be in fields. 

Once you find the element containing the date strings, in element properties change type from "empty" to "date" (seen here). 

Below, we see q1 is populated with the resulting date strings. The dates are not in ISO date format, but you can customize date formats in JSON.

You can hide, remove, or delete the separate month and year child elements . 

Reminder: For data processes, "Save" and "Run" the process after you are done editing the code view. To use the result of the process as the primary data for the project, you will need to set it as "Primary".
Data processes are specific to each project, and your code may not look identical to our example.

Was this article helpful?