I have got the date from the url query parameter but while setting it to the default value of field_date form element it is showing the date of the previous day and not the correct date.

use Drupal\Core\Datetime\DrupalDateTime;

function time_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
    // Get the current request object and a specific parameter from the URL query.
    $request = \Drupal::request();
    $parameter_value = $request->query->get('param');
    if ($form_id == 'node_time_entry_form'){
      $form['field_date']['widget'][0]['value']['#default_value'] = new DrupalDateTime($parameter_value);

1 Answer 1


Each date string is only valid for a specific timezone:

$date = new DrupalDateTime($date_string, 'America/Yellowknife');

If you don't specify a timezone the date/time object will be instantiated with the default timezone set for your system. So you might be able to solve the problem also by changing the site or user timezone settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.