What is the way to subtract datetime values in Twig in a view?

I have a starttime and endtime and want to calculate the difference bewtween the endtime and startime.

Comments

stinky created an issue. See original summary.

stinky’s picture

Issue summary: View changes
cilefen’s picture

Status: Active » Postponed (maintainer needs more info)
stinky’s picture

Not really, lol. I', completely new to Twig.

Here's what I tried-- and it doesn't work.

{% set var1 = field_cp1_time_out_1|date("H:i:s") %}
{% set var2 = field_cp1_time_in_1|date("H:i:s") %}

{% set difference = date(var1).diff(date(var2)) %}

{{ difference }}

cilefen’s picture

I strongly suggest you ask at https://drupal.stackexchange.com/.

cilefen’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

I urge you to use the better support resources.

cilefen’s picture

Status: Closed (outdated) » Postponed (maintainer needs more info)

Sorry: I didn't mean to just close this and I added a webform queue triage message although this is in Drupal Core! I've removed that. But the bottom line is that the Drupal Core issue queue is not ideal for support requests because they usually go unanswered here.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

nelo_drup’s picture

Same problem using diff produces the following error

Calling "diff" method on a "DateTime" object is not allowed in

cilefen’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.