How to edit a date field in a plugin So I want to edit date type filed on entry save, but I don't know how to format it. It times out on string and don't seem to accept DateTime objects either.

Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent $event) {
        if ($event-&gt;element instanceof \craft\elements\Entry) {
            $entry = $event-&gt;element;
            if($entry-&gt;sectionId == 16){
                $entry-&gt;rangeStart = date_create_from_format('d/M/Y:H:i:s', '01/01/2019:19:00:00');
                Craft::$app-&gt;elements-&gt;saveElement($entry);
            }
        }
});
The problem you have here is you are calling saveElement from EVENT_AFTER_SAVE_ELEMENT which is causing an infinite loop.

You need to set:

$entry-&gt;propagating = true;


Before calling the saveElement method.

Dates should be a PHP DateTime class, e.g:

$entry-&gt;rangeStart = new \DateTime('2019-04-10 19:00:00');