Add a method to format a DrupalDateTime object to DateFormatter

Created on 22 February 2018, over 6 years ago
Updated 30 January 2023, almost 2 years ago

There seems to be no connection between DateFormatter and DrupalDateTime.

AFAICT, given a DrupalDateTime, to format it as a defined format, I have to do this:

          $timestamp = $my_drupal_date_time->format('U');
          $formatted_date = \Drupal::service('date.formatter')->format($timestamp);

All the more ironic is that DateFormatter::format() internally converts to a DrupalDateTime:

    $date = DrupalDateTime::createFromTimestamp($timestamp, $this->timezones[$timezone], $create_settings);

These classes are both in the same component and yet don't work with each other!

We should either:

- add a method to DateFormatter that takes a DrupalDateTime
- add a method to DrupalDateTime that formats with DateFormatter

The first is probably better, since DrupalDateTime can't have service injected, and DateFormatter already works with DrupalDateTime.

Feature request
Status

Needs work

Version

10.1

Component
Base 

Last updated about 1 hour ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
  • DrupalWTF

    Worse Than Failure. Approximates the unpleasant remark made by Drupal developers when they first encounter a particular (mis)feature.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Production build 0.71.5 2024