Answers.Body: <p>The problem you have here is you are calling saveElement from EVENT_AFTER_SAVE_ELEMENT which is causing an infinite loop.</p>
  
  <p>You need to set:</p>
  
  <pre><code>$entry-&gt;propagating = true;
  </code></pre>
  
  <p>Before calling the saveElement method.</p>
  
  <p>Dates should be a PHP DateTime class, e.g:</p>
  
  <pre><code>$entry-&gt;rangeStart = new \DateTime('2019-04-10 19:00:00');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-11T10:41:02.690Z
Answers.Text: Ahh I was so set on date that I didn't see the loop, however setting $entry->propagating = true; didn't help. I've switched to before save event instead and now it works.
Answers.UserDisplayName: Lukigi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-10T18:01:54.380Z
Answers.LastActivityDate: 2019-04-10T18:01:54.380Z
Answers.UserDisplayName: Jay
Body: <p>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.</p>
  
  <pre><code>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);
              }
          }
  });
  </code></pre>
  
Tags: <plugin-development><field><datetime>
Title: How to edit a date field in a plugin
ThreadText: # How to edit a date field in a plugin
  
  (Asked by: username_0 on 2019-04-10)
  
  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->element instanceof \craft\elements\Entry) {
              $entry = $event->element;
              if($entry->sectionId == 16){
                  $entry->rangeStart = date_create_from_format('d/M/Y:H:i:s', '01/01/2019:19:00:00');
                  Craft::$app->elements->saveElement($entry);
              }
          }
  });
  
  ```
  
  **Tags:** plugin-development, field, datetime
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-04-10. Score: 1 | Accepted answer
  
  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->propagating = true;
  
  ```
  
  Before calling the saveElement method.
  
  Dates should be a PHP DateTime class, e.g:
  
  ```
  $entry->rangeStart = new \DateTime('2019-04-10 19:00:00');
  
  ```
  
  > **Comments:**
  > **username_0**: Ahh I was so set on date that I didn't see the loop, however setting $entry->propagating = true; didn't help. I've switched to before save event instead and now it works.  
  
  ---
  
  