Datetime iso date format timezone showing wrong time

Created on 5 September 2024, 4 months ago
Updated 16 September 2024, 3 months ago

Problem/Motivation

Showing wrong time datetime attribute in time tag

<div class="field field--name-field-release-date field--type-datetime field--label-hidden field__item"><time datetime="2024-09-05T04:09:23Z">05/09/2024 12:09</time>
</div>

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Datetime 

Last updated 2 days ago

Created by

🇮🇳India kmani

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @kmani
  • Status changed to Postponed: needs info 4 months ago
  • 🇮🇳India kmani

    Step 1: create a datatime field in any content type
    step 2: Render the datatime field in twig file.
    step 3: Check the time tag <time datetime="2024-09-05T04:09:23Z">05/09/2024 12:09</time>

    Both datatime and displayed time is different

    Solution: In DateTimeFormatterBase.php field formatter, we need update the below code to fix this issue.

    Current one in Core:

    protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
        // Create the ISO date in Universal Time.
        $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
        $this->setTimeZone($date);
    
        $build = [
          '#theme' => 'time',
          '#text' => $this->formatDate($date),
          '#attributes' => [
            'datetime' => $iso_date,
          ],
          '#cache' => [
            'contexts' => [
              'timezone',
            ],
          ],
        ];
    
        return $build;
      }

    Solution: We need to move above $this->setTimeZone($date); to iso_date field conversion.

    protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
        // Create the ISO date in Universal Time.
        $this->setTimeZone($date);
        $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
    
        $build = [
          '#theme' => 'time',
          '#text' => $this->formatDate($date),
          '#attributes' => [
            'datetime' => $iso_date,
          ],
          '#cache' => [
            'contexts' => [
              'timezone',
            ],
          ],
        ];
    
        return $build;
      }
  • Status changed to Active 4 months ago
  • Merge request !9434Datetime zone conversion issue fix. → (Open) created by kmani
  • Pipeline finished with Failed
    4 months ago
    Total: 427s
    #275410
  • Status changed to Needs work 4 months ago
  • Put the steps to reproduce in the issue summary. The current merge request fails a DateTime test.

  • 🇮🇳India anchal_gupta

    I am not able to reproduce this issue in my system.
    DateTime field format its working fine.
    As per reference I have attach screenshot.

Production build 0.71.5 2024