How to set default values for daterange widget in baseFieldDefinitions Everything is working in my code except the default values...

$rangedefaults['value'] = DrupalDateTime::createFromTimestamp(time());
$rangedefaults['end_value'] = DrupalDateTime::createFromTimestamp(time());

$fields['date'] = BaseFieldDefinition::create('daterange')
  -&gt;setLabel(t('Date and time'))
  -&gt;setDescription(t('When the activity takes place.'))
  -&gt;setRequired(false)
  -&gt;setDisplayOptions('view', array(
    'label' =&gt; 'above',
    'type' =&gt; 'string',
    'weight' =&gt; -4,
  ))
  -&gt;setDisplayOptions('form', array(
    'type' =&gt; 'daterange_default',
    'weight' =&gt; -4,
  ))
  -&gt;setDisplayConfigurable('form', TRUE)
  -&gt;setDisplayConfigurable('view', TRUE)
  -&gt;setDefaultValue($rangedefaults);


What is the proper way to set default values for this widget?
I think the problem is just the date format. If you look at the DateRangeItem class's generateSample method, it uses the DateTimeItemInterface::DATETIME_STORAGE_FORMAT format to prepare both value and end_value. You probably just need to do the same:

$rangedefaults['value'] = $rangedefaults['end_value'] = (new DrupalDateTime())-&gt;format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);


As an aside, since you're using the current time it feels like you might want the default value to be the time at which the entity is created. By setting it in the field definition, the default will always be the time you installed the field.

That might be what you want, but in case not, use setDefaultValueCallback instead, and add a method to the entity class which returns the default value.