3 namespace Drupal\datetime_range;
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
9 * Provides friendly methods for datetime range.
11 trait DateTimeRangeTrait {
14 * Creates a render array from a date object.
16 * @param \Drupal\Core\Datetime\DrupalDateTime $date
22 protected function buildDate(DrupalDateTime $date) {
23 if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
24 // A date without time will pick up the current time, use the default.
25 datetime_date_default_time($date);
27 $this->setTimeZone($date);
30 '#plain_text' => $this->formatDate($date),
42 * Creates a render array from a date object with ISO date attribute.
44 * @param \Drupal\Core\Datetime\DrupalDateTime $date
50 protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
51 if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
52 // A date without time will pick up the current time, use the default.
53 datetime_date_default_time($date);
56 // Create the ISO date in Universal Time.
57 $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
59 $this->setTimeZone($date);
63 '#text' => $this->formatDate($date),
66 'datetime' => $iso_date,