Datetime overview
This documentation needs work. See "Help improve this page" in the sidebar.
About
The Datetime module provides a Date field that stores dates and times. It also provides the Form API elements datetime and datelist which other modules can make use of. See the Field module documentation and the Field UI module documentation pages for general information on fields and how to create and manage them. For how to make use of the Form API elements in modules you program yourself, see the Form API documentation and the Form and render elements API reference documentation.
Uses
Managing and displaying date fields
The settings and the display of the Date field can be configured separately. See the Field UI module documentation for more information on how to manage fields and their display.
Displaying dates
Dates can be displayed using either the Plain or Default formatter. The Plain formatter displays the date in the ISO 8601 format. If you choose the Default formatter, you can choose a format from a predefined list that can be managed on the Date and time formats page.
Adjust datetime formats at Manage > Configuration > Regional and language > Date and time formats (path: admin/config/regional/date-time).
For more information, see Working With Content Types and Fields. For a list of the functions and their descriptions see the
API documentation.
Formatting dates programmatically
The Datetime module provides a DateFormatter service that can be used to format timestamps.
use Drupal\Core\Datetime\DrupalDateTime;
/** @var Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
// Get saved timestamp from a DateTimeFieldItemList field.
$date_value = $node->field_date->value;
$date_time = new DrupalDateTime($date_value, new \DateTimeZone('UTC'));
$timestamp = $date_time->getTimestamp();
// All other arguments are optional:
// See DateFormatterInterface for built-in options, or use machine name of a date format in config.
$type = 'medium';
// Custom PHP date format if $type="custom".
$format = '';
// > Drupal 8.8
// See https://www.drupal.org/node/3009387
$timezone = NULL;
$langcode = NULL;
$formatted = $date_formatter->format($timestamp, $type, $format, $timezone, $langcode);
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion