In Drupal 8, I have a Flag entity with a date field attached.

My code looks like this:

function MYMODULE_flagging_presave(FlaggingInterface $flagging) {
    if ($score == 0 || $score == 1) {
      $flagging->field_date_review = date("Y-m-d", strtotime('today'));
    else {
      $date = date("Y-m-d", strtotime("+$interval days"));
      $flagging->field_date_review = $date;

I checked and the dates get stored in the database like 2020-07-02.

I have a custom view that shows these flaggings, and I added a filter for nodes with a date field value of less than +1 days; this behaves as expected, so the Views module seems to understand the dates as I stored them.

But, when I try to add the date field, I get an empty display in Views.

Further debugging

When I render the flagging entity, the date field doesn't show a value, either.

Other fields set in ``hook_flagging_presave()` are displayed correctly; it's only the date field that doesn't have a value.


On the flagging entity's Manage display screen, I have set my date field to use the Plain format with no timezone offset. I also tried the Default format and several others, but nothing let me display the value.

There are no errors in the recent log messages.

To sum up: the date field has data in the database, which Views can see and use for filters. But, this date field cannot be displayed in Views or on the rendered flagging entity.

What do I need to do to get the date to be displayed?

  • Please add a screenshot of the Views config. Also, does the date show up on the regular view node page?
    – beltouche
    Jul 1, 2020 at 20:17
  • @beltouche Thanks, the problem doesn't seem to be Views. Updated. Jul 2, 2020 at 2:55
  • Have you tried setting the date using what is a more standard construct: $flagging->set('field_date_review', $date)?
    – beltouche
    Jul 2, 2020 at 3:02
  • @beltouche I tried that construct, same result; it is saved to DB but not displayed. Jul 2, 2020 at 4:09
  • 3
    Does this answer your question? Set date field in D8
    – 4uk4
    Jul 2, 2020 at 13:53

1 Answer 1


I found it-- the Date field can store dates or datetimes, and you have to change your code for each case.

When you add a Date field to an entity, you can choose Date (date) or Date and time (datetime) as the Date type.

More information in this blog post, but the basic idea is:

$node->set('field_date', date('Y-m-d', time()));
$node->set('field_datetime', date('Y-m-d\TH:i:s', time()));

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.