How to use daterange field programmatically?
If you programatically need to set a datetime range field when creating a node in a custom form submit handler for example, you need to to fill two values:
The start date value and the end date end_value.
$node = \Drupal\node\Entity\Node::create([
  'type' =&gt; $type,
  'title' =&gt; $title,
  'field_dates' =&gt; [
    'value'=&gt; '2018-02-02T12:12:12',
    'end_value' =&gt; '2018-05-05T12:12:12'
  ],
]);
$node-&gt;save();