I have a content type with a date field, which I convert to a specific date format in a preprocess function:

$variables['date'] =
\Drupal::service('date.formatter')->format($variables['node']->field_datum->date->getTimestamp(), 'long_date_without_time');

But as Germany is one hour ahead, a date like 2015-03-31 23:01:08 UTC converts to 1. April 2015. How can I convert this to our local time?

EDIT: I already set the correct timezone in the UI. And I just saw that the wrong dates are only shown when I set my system time between 11pm and 1 am...

  • Just a side note here, but your should really be using the field formatters / render system instead of in a preprocess. Timezone may not end up in the cache context here, so you may encounter weirdness b/c caching problems.
    – mpdonadio
    Commented Feb 2, 2017 at 2:18
  • I want to display the date before the page title and this is not possible with the normal field display...
    – 0711master
    Commented Feb 2, 2017 at 9:56

1 Answer 1


The date formatter service automatically converts it to whatever time zone you selected when you installed the Drupal site, admin/config/regional/settings.

Also, if you allow users to set their own timezone in user/1/edit, it would use that time.

So either you need to change the timezone in admin/config/regional/settings and/or in user/1/edit

  • I already had set on both sites the correct timezones. And I just saw that the wrong dates are only shown when I set my system time between 11pm and 1 am - this is really weird...
    – 0711master
    Commented Feb 2, 2017 at 2:07
  • If a TZ isn't supplied to DateFormatter, it will pull it from the PHP date_default_timezone_get(). If you use the field formatters, it honors the Drupal settings via drupal_get_user_timezone() or the override setting.
    – mpdonadio
    Commented Feb 2, 2017 at 2:10
  • Also, the config whether or not a user can use their own TZ is on admin/config/regional/settings. If that is set, then they can pick an option on their own user edit page.
    – mpdonadio
    Commented Feb 2, 2017 at 2:26
  • As I already said, the correct timezone is already set in the UI
    – 0711master
    Commented Feb 2, 2017 at 18:57
  • Which city or town do u live in and which timezone did u select?
    – No Sssweat
    Commented Feb 3, 2017 at 0:36

Your Answer

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

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