Answers.Body: <p>I achieved this by putting this into my variables file;</p>
  
  <pre><code>public function craftDate($date) {
  
       $dateObject = DateTime::createFromString($date,craft()-&gt;timezone);
  
       return $dateObject;
  
   }
  </code></pre>
  
  <p>and then calling this in my template by using;</p>
  
  <pre><code>{{ date({
      id: 'startDate',
      name: 'startDate',
      value: craft.formExport.craftDate(reconfiguredStartDate)
  }) }}
  </code></pre>
  
  <p>(The date had been reconfigured from 01/02/2017 to 2017-02-01)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-23T10:17:46.373Z
Answers.LastActivityDate: 2017-02-23T10:17:46.373Z
Answers.UserDisplayName: Jordan D
Answers.Body: <p>Unfortunately, what you're trying to do, is not possible without writing a simple plugin to return a <code>Craft\DateTime</code> object that the day picker can work with.</p>
  
  <p>The global variable <code>now</code>, is a <code>Craft\DateTime</code> object, which the date picker is happy to work with because it can call <code>localeDate()</code> on it.</p>
  
  <p>However, anything returned by <code>date</code> or <code>date_modify</code> twig filters, will be a <code>\DateTime</code> object, which lacks functionality that the date picker relies on.</p>
  
  <p>Your only option would be, to create a simple plugin that can take your date parameter, and return a <code>Craft\DateTime</code> object, which you can pass as a <code>value</code> to the date picker/date macro.</p>
  
  <p>This is one of those things that you think, <em>"this should be easy or at least possible"</em>, but I haven't found a <em>twig-only</em> solution yet.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-21T18:54:43.577Z
Answers.Text: This is one of the main reasons (along with about 100 others), that we've ditched the `Craft\DateTime` class in Craft 2 and are just using PHP's native DateTime everywhere in Craft 3.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-21T19:16:47.397Z
Answers.Text: @BradBell Craft 3 FTW:)
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-21T18:50:35.770Z
Answers.LastActivityDate: 2017-02-21T18:50:35.770Z
Answers.UserDisplayName: Selvin Ortiz
Answers.Body: <p>The <a href="https://github.com/marionnewlevant/craft-twig_perversion" rel="nofollow noreferrer">Twig Perversion</a> plugin has a <code>date_from_string</code> filter that turns a string into a <code>Craft\DateTime</code> object.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-23T06:42:43.743Z
Answers.LastActivityDate: 2017-02-23T06:42:43.743Z
Answers.UserDisplayName: Marion Newlevant
Body: <p>I'm trying to use Craft's native datepicker with a plugin I've made for the admin section. </p>
  
  <p>I've got some code that works;</p>
  
  <pre><code>{% from "_includes/forms" import date %}
  
  &lt;h3&gt;Start Date&lt;/h3&gt;
  
  {{ date({
      id: 'startDate',
      name: 'startDate',
      value: now
  }) }}
  </code></pre>
  
  <p>but I can't seem to get it to work with any other date, no matter what I do. </p>
  
  <p>In my URL I have;</p>
  
  <pre><code>startDate=09/02/2017
  </code></pre>
  
  <p>and in my twig template I have;</p>
  
  <pre><code>{% set startDate = craft.request.getParam('startDate') %}
  </code></pre>
  
  <p>What do I need to do to <code>startDate</code> in order to get the date picker to read that date?</p>
  
Tags: <plugin-development><templating><datetime>
Title: Making Craft\DateTime Object from string in Twig
ThreadText: # Making Craft\DateTime Object from string in Twig
  
  (Asked by: username_0 on 2017-02-21)
  
  I'm trying to use Craft's native datepicker with a plugin I've made for the admin section.
  
  I've got some code that works;
  
  ```
  {% from "_includes/forms" import date %}
  
  <h3>Start Date</h3>
  
  {{ date({
      id: 'startDate',
      name: 'startDate',
      value: now
  }) }}
  
  ```
  
  but I can't seem to get it to work with any other date, no matter what I do.
  
  In my URL I have;
  
  ```
  startDate=09/02/2017
  
  ```
  
  and in my twig template I have;
  
  ```
  {% set startDate = craft.request.getParam('startDate') %}
  
  ```
  
  What do I need to do to `startDate` in order to get the date picker to read that date?
  
  **Tags:** plugin-development, templating, datetime
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2017-02-23. Score: 2 | Accepted answer
  
  I achieved this by putting this into my variables file;
  
  ```
  public function craftDate($date) {
  
       $dateObject = DateTime::createFromString($date,craft()->timezone);
  
       return $dateObject;
  
   }
  
  ```
  
  and then calling this in my template by using;
  
  ```
  {{ date({
      id: 'startDate',
      name: 'startDate',
      value: craft.formExport.craftDate(reconfiguredStartDate)
  }) }}
  
  ```
  
  (The date had been reconfigured from 01/02/2017 to 2017-02-01)
  
  
  ---
  
  ### Answer by username_1 on 2017-02-21. Score: 2
  
  Unfortunately, what you're trying to do, is not possible without writing a simple plugin to return a `Craft\DateTime` object that the day picker can work with.
  
  The global variable `now`, is a `Craft\DateTime` object, which the date picker is happy to work with because it can call `localeDate()` on it.
  
  However, anything returned by `date` or `date_modify` twig filters, will be a `\DateTime` object, which lacks functionality that the date picker relies on.
  
  Your only option would be, to create a simple plugin that can take your date parameter, and return a `Craft\DateTime` object, which you can pass as a `value` to the date picker/date macro.
  
  This is one of those things that you think, *"this should be easy or at least possible"*, but I haven't found a *twig-only* solution yet.
  
  > **Comments:**
  > **username_2**: This is one of the main reasons (along with about 100 others), that we've ditched the `Craft\DateTime` class in Craft 2 and are just using PHP's native DateTime everywhere in Craft 3.  
  > **username_1**: @username_2 Craft 3 FTW:)  
  
  ---
  
  ### Answer by username_3 on 2017-02-23. Score: 0
  
  The [Twig Perversion](https://github.com/username_3/craft-twig_perversion) plugin has a `date_from_string` filter that turns a string into a `Craft\DateTime` object.
  
  
  ---
  
  