Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create a node With date field

By editor , 4 December, 2015
Question

How to create and format a date field.

It is same as normal node creation but must be in format "Y-m-d" (YYYY-MM-DD)

Example:

$dateTime = \DateTime::createFromFormat('Y-m-d','2000-01-30');
$newDateString = $dateTime->format('Y-m-d\TH:i:s');
$node = \Drupal\node\Entity\Node::create(array(
          'type' => 'article',
          'title' => 'The title',
          'langcode' => 'en',
          'uid' => 1,
          'status' => 1,
          'body' => array('The body text'),

          //'field_date' => array("2000-01-30"),
          'field_date' => $newDateString,
    ));
$node->save();

Tags

  • Drupal 8
  • Code
  • Node
  • Add new comment
Create a node programmatically in Drupal 8

Comments4

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

John Tang (not verified)

10 years 2 months ago

I tried 'field_date' =>…

I tried 'field_date' => array("2000-01-30"), for drupal 8.1.8 but seem it's not working.

  • Reply

Roman Bensman (not verified)

10 years ago

Node with date field

DATETIME_DATETIME_STORAGE_FORMAT, which is 'Y-m-d\TH:i:s'

                  $dateTime = \DateTime::createFromFormat('Y-m-d','2000-01-30');
                  $newDateString = $dateTime->format('Y-m-d\TH:i:s');

                  and

            'field_date' => $newDateString,

https://www.drupal.org/node/2795353

  • Reply

editor

10 years ago

In reply to Node with date field by Roman Bensman (not verified)

Thankyou !!!

Thankyou !!!

  • Reply

Juan Ceballos (not verified)

9 years ago

In reply to Node with date field by Roman Bensman (not verified)

Thx

Thx

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer