HOW TO

Calculate the number of weekdays between two date fields using Python in ArcGIS Pro

Last Published: April 6, 2026

Summary

In ArcGIS Pro, Python can be used to calculate the number of weekdays between two date fields, supporting temporal analysis that excludes weekends. This is useful for business workflows or tracking activities based on working days.

This article provides the workflow to calculate the number of weekdays between two date fields using Python in ArcGIS Pro.

Procedure

  1. In ArcGIS Pro, open the project.
  2. Open the attribute table containing the two date fields.
  3. Add a new integer field in the attribute field. Refer to ArcGIS Pro: Add Field (Data Management) for instructions. In this example, the newly added field is NewField.
  4. In the attribute field, right-click the newly added field and click Calculate Field.
The Calculate Field option
  1. In the Calculate Field window, for Expression Type, ensure the default Python option is selected from the drop-down list.
  2. For Expression, copy the code below and paste it in the expression box. Replace <fieldName1> and <fieldName2> by double-clicking the fields in the Fields list. In this example, the StartDate and EndDate fields are used.
count_weekdays(!<fieldName1>!,!<fieldName2>!)
The Calculate Field window
  1. In the Code Block box, copy and paste the code below. Replace <fieldName1> and <fieldName2> with their respective field names.
from datetime import datetime, timedelta

def count_weekdays(<fieldName1>, <fieldName2>):
    # Convert input to datetime if it's not already
    if isinstance(<fieldName1>, str):
         <fieldName1> = datetime.strptime(<fieldName1>, "%m/%d/%Y")
    if isinstance(<fieldName2>, str):
         <fieldName2> = datetime.strptime(<fieldName2>, "%m/%d/%Y")
    
    # Initialize count
    count = 0
    current_date = <fieldName1>
    
    while current_date <= <fieldName2>:
        # Count only weekdays (Monday to Friday)
        if current_date.weekday() < 5:
            count += 1
        current_date += timedelta(days=1)
    
    return count

Below is the complete working script for this example.

from datetime import datetime, timedelta

def count_weekdays(start_date, end_date):
    # Convert input to datetime if it's not already
    if isinstance(start_date, str):
        start_date = datetime.strptime(start_date, "%m/%d/%Y")
    if isinstance(end_date, str):
        end_date = datetime.strptime(end_date, "%m/%d/%Y")
    
    # Initialize count
    count = 0
    current_date = start_date
    
    while current_date <= end_date:
        # Count only weekdays (Monday to Friday)
        if current_date.weekday() < 5:
            count += 1
        current_date += timedelta(days=1)
    
    return count
  1. Click Verify The Verify iconto validate the expression.
  2. Click Apply > OK.

The attribute table below displays the calculated number of weekdays in the new field in ArcGIS Pro.

The calculated number of weekdays in the attribute table

Article ID: 000034784

Software:
  • ArcGIS Pro

Get support with AI

Resolve your issue quickly with the Esri Support AI Chatbot.

Start chatting now

Get help from ArcGIS experts

Contact technical support

Start chatting now

Go to download options