Programmatically set the value of a date/Time field I am trying to copy the value of a Date/Time Webform element to a Date/Time field set on the article node.

Below is the source code of the Date/Time Webform element:

expiry_date:
  '#type': datetime
  '#title': 'Expiry Date'
  '#title_display': before
  '#required': true
  '#required_error': 'You must add an expiry date'
  '#date_date_format': ''
  '#date_year_range': '2019:2100'
  '#date_time_format': ''


Below is my custom handler:

// Get an array of the values from the submission.
    $values = $webform_submission-&gt;getData();

    // Get 'Expiry Date' Value.
    $ExpiryDate = $values['expiry_date'];
    date_default_timezone_set('UTC');   // Sett the time to 'UTC' time zone.

    // Format the "Expiry Date" to return "Y-m-d\TH:i:s".
    //$FormattedExpiry = date('Y-m-d\TH:i:s', $ExpiryDate);


// Create a new "Article" node.
    $article = \Drupal\node\Entity\Node::create([
        'type' =&gt; 'article',
    // Set the node field values.        
        'title' =&gt; $Title,
        'langcode' =&gt; 'en',
        'uid' =&gt; $CurrentUid,
        'status' =&gt; '1',
        'created' =&gt; $current_time,
        'field_expiry_date' =&gt; $ExpiryDate, // $FormattedExpiry,
        ]);

// Save the created node.
    $article-&gt;save()


But I am facing the below error:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'field_expiry_date_value' at row 1: INSERT INTO {node__field_expiry_date} (entity_id, revision_id, bundle, delta, langcode, field_expiry_date_value) VALUES (
:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5);
Array ( 
[:db_insert_placeholder_0] =&gt; 84
[:db_insert_placeholder_1] =&gt; 88
[:db_insert_placeholder_2] =&gt; article
[:db_insert_placeholder_3] =&gt; 0
[:db_insert_placeholder_4] =&gt; en
[:db_insert_placeholder_5] =&gt; 2021-05-25 11:26:02 +03:00 
)
in Drupal\Core\Entity\Sql\SqlContentEntityStorage-&gt;saveToDedicatedTables() (line 1414 of /home/username/public_html/example.com/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
The expiry_date from webform return something like 2021-05-25 11:26:02 +03:00 so you should to format it before you passe it to create like the following:

// Get 'Expiry Date' Value.
$ExpiryDate = $values['expiry_date'];
// Format the "Expiry Date" to return "Y-m-d\TH:i:s".
// Here use strtotime to format date correctly 
$FormattedExpiry = date('Y-m-d\TH:i:s', strtotime($ExpiryDate));


Then passe $FormattedExpiry instead of $ExpiryDate:

$article = \Drupal\node\Entity\Node::create([
    'type' =&gt; 'article',
   // Set the node field values.        
    'title' =&gt; $Title,
    'langcode' =&gt; 'en',
    'uid' =&gt; $CurrentUid,
    'status' =&gt; '1',
    'created' =&gt; $current_time,
    'field_expiry_date' =&gt; $FormattedExpiry,
    ]);


Update: your date already has +3h, if you want to not add 3 hours to your time there is two options i guess:


Change you webform field settings to work with UTC to get the value of $values['expiry_date'] something like 2021-05-25 11:26:02 without +03:00 
Remove the +03:00 before date conversion from $values['expiry_date'] by change the line $ExpiryDate = $values['expiry_date']; with something like:

$ExpiryDate = explode('+', $values['expiry_date'])[0];