Set a date programmatically Once a page is saved I want to put the current time (I know I can do it by the form, but I need to do it programmatically). My code:

function mymodule_node_presave(EntityInterface $entity) {
    $node_type = $entity-&gt;bundle();
    if ($node_type === 'page') {
            $date = date('Y-m-d 00:00:00', time());
            $entity-&gt;field_mydate-&gt;value = $date;
            $entity-&gt;body = $date;
       }
}


It changes the body correctly but not the date field.
I tried all those, none works. I can't understand where is the problem.
Solved the issue.
In my case, after debugging the value, I guessed the right code could be:

$date = date('Y-m-d\T00:00:00', time());


And it is. I'm not sure what the date format depends on. For sure it doesn't depends on the form display format since in the I'm using the Italian one (d/m/Y).