HOW TO

Extract month names from a date field using ArcGIS Arcade in Map Viewer

Last Published: October 1, 2024

Summary

In Map Viewer for ArcGIS Online and ArcGIS Enterprise, extracting month names from a date field and populating a string field with these names is crucial for enhancing data analysis and visualization. This process is useful for categorizing and filtering data based on specific months, facilitating temporal analysis and trend identification.

This article provides the workflow for extracting month names from a date field named 'Date_Joined' and populating them in a new string field named 'Ext_Month' using an Arcade expression in Map Viewer.

Procedure

  1. In Map Viewer, click the desired hosted feature layer. On the item details page, click the Data tab.
Note:
Skip to Step 3 if the script is being run on an existing field.
  1. In the Table view, create a new field. Refer to ArcGIS Online: Add a field or Portal for ArcGIS: Add a field for instructions. Ensure String is selected from the Type drop-down list. In this example, a field named 'Ext_Month' is created.
  2. Click the field header and select Calculate. In this example, 'Ext_Month' is selected.
  3. In the Calculate Field dialog box, click Arcade.
  4. In the Arcade Calculator dialog box, specify the Expression box as follows to extract month names from a date field:
    1. Define the date field. Replace <field_name> with the name of the date field. In this example, 'Date_joined' is used.
var date = $feature.<field_name>;
  1. Check for the availability of the date and extract the month number from the date.
if (!IsEmpty(date)) {

    var monthNumber = Text(Month(date) + 1, '00'); 
  1. Define an object for the months with corresponding two-digit values.
    var months = {
        "January": "01",    
        "February": "02",
        "March": "03",
        "April": "04",
        "May": "05",
        "June": "06", 
        "July": "07",
        "August": "08",
        "September": "09",
        "October": "10",
        "November": "11",
        "December": "12"
    };
  1. Iterate through the months object to find the corresponding month name.
    for (var key in months) {
        if (months[key] == monthNumber) {
            return key;  
        }
    }
    
    return "Invalid month number";  
} else {
    return "Date not available";  
}

Below is the full working code of the Arcade expression.

var date = $feature.Date_joined;

if (!IsEmpty(date)) {

    var monthNumber = Text(Month(date) + 1, '00'); 

    var months = {
        "January": "01",    
        "February": "02",
        "March": "03",
        "April": "04",
        "May": "05",
        "June": "06", 
        "July": "07",
        "August": "08",
        "September": "09",
        "October": "10",
        "November": "11",
        "December": "12"
    };

    for (var key in months) {
        if (months[key] == monthNumber) {
            return key;  
        }
    }
    
    return "Invalid month number";  
} else {
    return "Date not available";  
}
  1. Click Test to verify the expression, and click OK to apply the query.
The Arcade Calculator dialog box with the Arcade expression to extract month names from the date field

The image below shows the month extracted from the date field, which is then populated as text in the newly created string field.

The table displaying the extracted months from the date field in a string field

Article ID: 000033574

Software:
  • ArcGIS Online
  • Portal for ArcGIS
  • ArcGIS Enterprise 11 1
  • ArcGIS Enterprise 11 3
  • ArcGIS Enterprise 11 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options