I have a custom entity with (among others) a field defined this way:

$fields['datedistribution'] = BaseFieldDefinition::create('datetime')
  ->setLabel(t('Date distribution'))
  ->setSetting('datetime_type', 'date')
  ->setDefaultValue(array(0 => array(
    'default_date_type' => 'now',
    'default_date' => 'now',
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'weight' => 2,
  ->setDisplayOptions('form', array(
    'weight' => 2,
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

The values of this field are stored in the database in YYYY-MM-DD format.
I designed a View on this custom entity displaying (among others) this 'datedistribution' field and I use it as a filter criterion. I would like to a filter condition like "from now to now+1 year". The available operators being enter image description here

how can I do that?

  • i don't think you can, core doesn't offer useful date field filters yet. The answer is for Drupal 7. – Berdir Sep 24 '16 at 13:47
  • Well, you can if you implement your own plugin, which isn't very complicated, but not in the UI. – Berdir Sep 24 '16 at 13:47

I believe I can help, if I understand your question correctly. First you should make sure that when you set the filter up, it's in Select form. select form

Then you can select the operator as "is between" Then you select "Enter a relative date" in the dropdown, and you can put now and now+1 year. Not sure if your date fields have a start and end date...


  • I'm afraid I don't have the step you're mentioning... I go directly from 'Add filter criteria' (see screen capture i.stack.imgur.com/QpN5q.png) to 'Configure filter criterion' (see screen capture in my question) – gbmapo Sep 24 '16 at 13:45
  • Hmm are you not using the Date module for dates? Not sure how to handle if a custom date field sorry. – KGates Sep 25 '16 at 18:46

NOW() will not work

I have used this method first fetch date from database then put date to condition

$time = \Drupal::database()->query( "SELECT NOW() as currtime " );
$timere = $time->fetchAll(); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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