4

In a node template, if I use the following:

{{ content.field_event_start_date }}

The correct localized date and time will be displayed. For example:

Tuesday, February 14, 2017 @ 9:00am EDT

However, if I only want to display the time part and use the following:

{{ node.field_event_start_date.value|date("g:ia") }}

The incorrect time is displayed:

1:00pm

There appears to be a +4h offset when using the TWIG date() filter.

Drupal's Timezone is set to "America/New York".

How can I display only the time part so that it correctly displays the following?

9:00am

Also, can you please explain what conversion Drupal is performing on the time under the covers?

Thank you.

2 Answers 2

3

The proper thing to do here is to configure your field output to use the proper formatter. On the Manage Display for your content type, eg admin/structure/types/manage/page/display, change the formatter for your field_event_start_date field to Custom, then click on the gear and change the format to 'g:ia'. You should then be able to use {{ content.field_event_start_date }} in your template.

The standard Twig date() filter doesn't use the date.formatter service, so timezones and localization settings will not be honored.

TwigExtension defines a custom filter for Drupal that will call out to DateFormatter::format() called format_date(). This should work

{{ node.field_event_start_date.value|format_date("custom", "g:ia") }}

This apparently only works for Timestamp fields and not datetime or daterange fields.

6
  • I'm not 100% certain about the syntax there, as I don't think I have ever used this directly.
    – mpdonadio
    Feb 14, 2017 at 19:49
  • 1
    Thanks for your reply and explanation. I tried your suggestion, but it seems that it will only work if the particular field is of type "timestamp". The field that I am using is of type "date". Here's what Drupal output when I tried: Exception: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp() (line 172 of core/lib/Drupal/Component/Datetime/DateTimePlus.php). Anything else that I can try? Thanks again!
    – kvjava1
    Feb 15, 2017 at 0:57
  • @kvjava1 Sorry about that; Twig is pushing the limits of what I do in Drupal 8 and I am not sure how to extract the timestamp in a template. I assume using the custom formatter, and using the 'g:ia' format isn't an option?
    – mpdonadio
    Feb 15, 2017 at 1:34
  • It's ok. Can you clarify (and/or give an example of) what you mean by "using the custom formatter, and using the 'g:ia' format"?
    – kvjava1
    Feb 15, 2017 at 2:01
  • I even tried converting the "datetime" to a "timestamp" and then doing {{ node.field_event_start_date.value|date('U')|format_date("custom", "g:ia", "America/New_York") }}, but that didn't work either. :( If I use "PST" for the timezone instead of "America/New_York", then it works, but I'm in "EST" so I don't like that hack. :( Trying to figure out what's going on with Drupal internals...?
    – kvjava1
    Feb 15, 2017 at 2:05
0

You can preprocess your node in THEME.theme (or in a module if you prefer, note that my field is 'field_date' you will need to adapt to your field machine name).

I copy the field using another display formatter. I'm using datetime_custom formatter, but you can use whatever you want.

function THEME_preprocess_node(&$variables) {
  $node = $variables['node'];

  // Extract the hour from the field_date so we can easily display concert_time
  if(!empty($node->field_date)){
    $variables['concert_time'] = $node->field_date->view([
      'type' => 'datetime_custom',
      'settings' => ['date_format' => 'H:i'],
    ]);
  }
}

Then in your twig

{{ concert_time }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.