twig error: Unexpected token "punctuation" of value ":" ("end of statement block" expected)

I am attempting to take all of the start dates of my coworkers and calculate a total # of years of experience. However I am getting the following error

Twig\Error\SyntaxError: Unexpected token "punctuation" of value ":" ("end of statement block" expected). in Twig\TokenStream->expect() (line 44

{% if title %}
  <h3>{{ title }}</h3>
{% endif %}

{% set total_seconds = 0 %} {# Initialize a variable to store the total seconds #}

{% for row in rows %}
  {%
    set row_classes = [
      default_row_class ? 'views-row',
    ]
  %}

  {# Assuming 'row.content' contains a date field named 'field_start_date' #}
  {% set date_str = row.content['#view'].style_plugin.getField(row_index, 'field_start_date') %}

  {% if date_str %}
    {% set today = date('now') %}
    {% set date_object = date(date_str) %}

    {# Calculate the difference in seconds using Drupal's date formatter service with createAttribute for options #}
    {% set options = create_attribute() %}
    {% set options = options.setAttribute('granularity', 1) %} 
    {% set diff_seconds = Drupal::service('date.formatter')->formatDiff(date_object.getTimestamp(), today.getTimestamp(), 'en', options) %}

    {% set total_seconds = total_seconds + diff_seconds %} {# Add the calculated seconds to the total #}

    <div{{ row.attributes.addClass(row_classes) }}>
      {{- row.content -}}
    </div>
  {% else %}
    <div{{ row.attributes.addClass(row_classes) }}>
      {{- row.content -}}
    </div>
  {% endif %}

{% endfor %}

{% if total_seconds > 0 %}
  {% set total_years = (total_seconds / (365 * 24 * 60 * 60))|round(2) %} {# Convert seconds to years and round #}
  <p>Total combined years since all dates: {{ total_years }}</p> {# Display the final result #}
{% endif %}

commenting out this line {#% set diff_seconds = Drupal::service('date.formatter')->formatDiff(date_object.getTimestamp(), today.getTimestamp(), 'en', options) %#} removes any error, but doesn’t return the single value that I am expecting.

Twig

{% if date_str %}
  {% set today = date('now') %}
  {% set date_object = date(date_str) %}

  {# Calculate the difference in seconds using Drupal's date formatter service with createAttribute for options #}
  {% set options = create_attribute() %}
  {% set options = options.setAttribute('granularity', 1) %}
  {% set diff_string = Drupal::service('date.formatter')->formatDiff(date_object.getTimestamp(), today.getTimestamp(), 'en', options) %}

  {# Extract the numerical value from the formatted string #}
  {% set diff_parts = diff_string|split(' ') %}
  {% set diff_seconds = diff_parts[0]|number_format %}

  {% set total_seconds = total_seconds + diff_seconds %}
  {% set total_years = (total_seconds / (365 * 24 * 60 * 60))|round(2) %}
  <p>Total combined years since all dates: {{ total_years }}</p>
{% endif %}

Explanation of the changes:

  1. Store the formatted string: The formatDiff method’s result is now stored in the diff_string variable.
  2. Extract the numerical value: The diff_string is split into an array using split(' ') . The first element of the array (containing the numerical value) is extracted and converted to a number using number_format .
  3. Calculate total seconds and years: The extracted diff_seconds value is added to the total_seconds variable, and the total_years calculation remains unchanged.

This modification should provide the correct total number of years based on the start dates in your data.