Date is wrong in storage but correct on display? I am trying to programmatically load a node and get its date time value. The default format is HTML5 date, long date. I load it and I get the field value with the following code.

dpm($record['nid_value']);
$entity = \Drupal\node\Entity\Node::load($record['nid_value']);
dpm($entity-&gt;get('field_date_range')-&gt;value);


The output is the following one.

740
2018-02-23T02:00:00
734
2018-02-23T00:00:00


Why is it stored as 2018-02-23T02:00:00 when the real date is Thursday - February 22, 2018 - 4:00pm to 5:00pm?

The field type is date range; the display type is smart range. When you view the node, it looks perfect. The display is correct. If I create a view, it also outputs the incorrect date, no matter what format I chose on the field output. Time zone has been forced.



Can anyone point me in the right direction here? 

UPDATE - Here was the solution to my problem

$nid = 411;
$entity = \Drupal\node\Entity\Node::load($nid);
$dval = $entity-&gt;get('field_date_range')-&gt;value();
$dt = new DateTime($dval, new DateTimeZone('UTC'));
$dt-&gt;setTimezone(new DateTimeZone('America/Los_Angeles'));
$date_to_print = $dt-&gt;format('g:ia \o\n l jS F Y');
//$date_to_print = $dt-&gt;format('m-d-Y h:m:i');
echo $date_to_print;
//output: 12:00pm on Saturday 24th February 2018
Datetime and Daterange fields are stored in UTC in the database (the storage) and converted to the time zone on output. This is done because the system supports users across the world, so everything is normalized to UTC.

Basically, when you use the UI the field widget takes the input value, and converts it from the user's time zone per system configuration to UTC (see DateTimeWidgetBase::massageFormValues()).  The actual format in storage is the ISO equivalent to the PHP format Y-m-d\TH:i:s.

When output, it takes loads the UTC value, and converts it to the user's time zone per system configuration or uses the forced time zone (the time zone override).  See DateTimeDefaultFormatter::formatDate() for an example, and also DateFormatter::format().

By default, views output field values using the formatters, so the same rules apply.
It sounds like you may have an override in the view settings for the field.