Example of using this with a date field from a node in order to get it to format the date in the correct timezone

use Drupal\Core\Datetime\DrupalDateTime;

$date = new DrupalDateTime($mynode->field_my_date_field->value, 'UTC');
$formatted_date = \Drupal::service('date.formatter')->format($date->getTimestamp(), 'custom', 'F j, Y h:ia');
Tags: 
Drupal 8

Recent Comments

Forward by Dries Buytaert, Founder and Project Lead, Drupal; CTO Acquia.

Order now from:

Managing Expectations Podcast with DrupalEasy

Author Interview http://tinyurl.com/8a8nxno

We provide the most Shiny Drupal Bits & Pixels

Since Drupal 4.x