I've got a Date field in Drupal that I want to preprocess. Unfortunately


Gives me a value off by one hour (8:00 when expected is 9:00). I believe the issue has something to do with the way Drupal stores the date vs how it displays it later (taking into account Timezones). I tried passing in the default timezone to the DateTime object but that didn't help. I also tried using the \Drupal::service('date.formatter') to format the date but the result is still off.

When debugging I found that by using:


I can see the expected date under a protected DateTimeObject property (see screenshot). enter image description here

Does anyone know how to extract it or maybe there's something else that I'm missing? I'm running Drupal 8.1.9

  • Please post the code you wrote using the 'date.formatter' service, which wasn't working for you. It seems like that approach should work.
    – hampercm
    Sep 21 '16 at 14:12

When displaying a date field, Drupal typically converts it to the timezone of the currently logged in user. If no user is logged in, the site's default timezone is used. Perhaps you're logged in as a user that has a different timezone set?

Also, note that the time is internally stored as UTC, so you'll see a time that is off by an hour when simply printing the date's value or using a debugger. If you want to print the time in the user's timezone, try something like:


This will use the user's timezone, or fall back to the site's default timezone.

  • Thank you for your suggestions but both the site's default timezone and the user's timezone I'm logged in as are the same ('Europe/Dublin'). I also cleared the cache but it didn't help. Sep 20 '16 at 14:49
  • 1
    Note that the time is stored by Drupal as GMT; that's what you'll see when you simply print out $node->field_date_time->value, or in a debugger. I'll add a bit more to my answer as I think I better understand what's going on.
    – hampercm
    Sep 20 '16 at 15:21
  • This is def a TZ problem. Dublin in in in DST right now, so there is a +1hr difference from UTC. Does the field render out wrong on the node with the default formatter, or is this just when you are trying to do it manually?
    – mpdonadio
    Sep 20 '16 at 16:52

The value get with $value = $node->field_date_time->value; is the datetime value stored in UTC.

If your convert this string to a DateTime object

$date = new DrupalDateTime($value)

Then you've got a Datetime object with the default Timezone set on the site (Europe/Dublin). To be sure to have the the correct datetime object, you should create a DateTime object with the UTC TimeZone

$date = new DrupalDateTime($value, new DateTimeZone('UTC'));

And then your date object could be format correctly by the date.formatter service which will convert the datetime to the Default TimeZone site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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