I'm using a date field on my custom type, if I use "select" widget for node editing, I can choose only between "YMD","MDY" and "DMY" formats.

Is there a way to add custom formats? I need an "only year" format.

1 Answer 1


By default, no. The relevant code is in \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeDatelistWidget::formElement().

What you want is usually called a "partial date", which is a bit of an oxymoron. In Drupal, datetime fields are stored as DrualDateTime objects, which derive from \DateTime so have both a full date and a proper time and timezone associated with them.

A partial date (year, year+month), has to make assumptions about the rest of the parts. For example, the date-only field currently sets the time to noon UTC. A year only field would have to do something similar.

You may be able to create your own widget for the datetime field to do this. You could take DateTimeDatelistWidget as a starting point, and trim it down to be just the year. Then, you would need a masssageFormValues() to set the rest of the parts and then call the parent method (because DateTimeWidgetBase::masssageFormValues() does stuff, too). This may or may not work, it is just the idea that popped into my head.

There is an open issue about this, https://www.drupal.org/node/2703037, that may or may not be implemeneted.

  • Ok, thanks. However, on D7 there is this option and Drupal completes the date, if I configure date widget in a "year only" format, even if it saves on db a complete datetime value. I'll try a new widget!
    – arrubiu
    Jun 25, 2016 at 15:19
  • 1
    @arrubiu The datetime module in core plus related code scattered through core is not a straight port of the Drupal 7 Date module and friends. It is a trimmed down version, and we are working to bring in some more features. However, some some features from those modules are going to have to live in contrib. A lot of the Drupal 7 Date modules had "do-it-all" plugins which are really hard to maintain long-term. Since it is pretty easy to add widgets and formatters now, we are trying to abandon the "do-it-all" approach and just provide the building blocks.
    – mpdonadio
    Jun 25, 2016 at 15:25
  • 1
    Sorry about that. I was thinking D7; I've removed my answer. Jun 25, 2016 at 15:25
  • @MPD, it's ok, a usually develop custom modules on D7, I'm starting on D8 and so I've to study a lot ;) Thanks!
    – arrubiu
    Jun 25, 2016 at 15:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.