Answers.Body: <p>In Twig, the <code>date_modify</code> filter returns a DateTime object. Also, <code>date</code> is not just a filter, but also a function. The <code>date</code> function accepts any a DateTime object or a string in a <a href="http://www.php.net/manual/en/datetime.formats.php">supported format</a>, and returns a DateTime object. Example:</p>
  
  <pre><code>{{ date("+2 years") }}
  </code></pre>
  
  <p>This should give you a DateTime object set for 2 years from when it's triggered.</p>
  
  <p><strong>Why is localeTime missing?</strong></p>
  
  <p>The <code>now</code> variable is not a regular DateTime object from PHP. It's actually an extended class ( \Craft\DateTime ). So it won't have all the convenience functions you get from <code>now</code> or a date field from an entry. Instead you would need to do it manually (I'm not sure if you can overwrite a function or filter in Twig). Fortunately, the locale's format string is available as a variable.</p>
  
  <pre><code>{% set myDatetimeString = '2014-06-24 23:40' %}
  {% set format = craft.i18n.getTimepickerJsFormat() %}
  {{ myDatetimeString|date(format) }}
  </code></pre>
  
  <p>You can even shorten it like this:</p>
  
  <pre><code>{% set myDatetimeString = '2014-06-24 23:40' %}
  {{ myDatetimeString|date(craft.i18n.getTimepickerJsFormat()) }}
  </code></pre>
  
  <p>To replicate localeDate, use this:</p>
  
  <pre><code>{% set myDatetimeString = '2014-06-24 23:40' %}
  {{ myDatetimeString|date(craft.i18n.getDatepickerJsFormat()) }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T21:59:35.047Z
Answers.Text: These DateTime objects don't work with `localeTime()` methods for me.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T22:48:06.613Z
Answers.Text: @ChristianSeelbach I gotcha now. I have edited my response to fill the missing information.
Answers.UserDisplayName: Bryan Redeagle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T23:07:27.230Z
Answers.Text: Great Bryan, thanks a lot! But that first sentence confuses me, sure you mean `now` variable?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T23:53:53.720Z
Answers.Text: Yes. `now` is listed as a Global Variable in the [Docs](http://buildwithcraft.com/docs/templating/global-variables).
Answers.UserDisplayName: Bryan Redeagle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-14T11:00:32.233Z
Answers.Text: Bryan, does this still work for you? Tried it again today and noticed that `craft.i18n.getDatepickerJsFormat()` outputs a format like this `dd/mm/yyyy` instead of `d/m/Y`?! Which makes it useless for the date filter :(
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-14T11:59:14.770Z
Answers.Text: I haven't used much date stuff recently, but I'll be creating a simple Events section tonight for a new site. I'll check it out then.
Answers.UserDisplayName: Bryan Redeagle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T18:54:33.560Z
Answers.LastActivityDate: 2014-06-24T22:46:24.680Z
Answers.LastEditDate: 2014-06-24T22:46:24.680Z
Answers.UserDisplayName: Bryan Redeagle
Answers.Body: <p>This works for me, starting with a string:</p>
  
  <pre><code>{% set yearbegins = '1 January ' ~ year %} // getting year from a url (dynamic route)
  {{ yearbegins | date("U") }} // outputs the date in Unixtime
  </code></pre>
  
  <p>Are you saying this is not a real datetime object, and it's just the date filter making it look like it? I'm finding some unpredictability, too, as you know.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T21:58:36.523Z
Answers.Text: In my question I already described that the `date` filter works for me to. It's the `localeTime()` method I have problems with. And in regards to that DateTime problem you encountered, I think I solved that one! See my [new answer](http://craftcms.stackexchange.com/a/447/125). ;)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T16:07:45.887Z
Answers.LastActivityDate: 2014-06-24T16:12:50.527Z
Answers.LastEditDate: 2014-06-24T16:12:50.527Z
Answers.UserDisplayName: Clive Portman
Body: <p>I experienced that there is a difference between real DateTime objects (as <code>now</code> is one) and strings composed to "fake DateTime objects" (ie. <code>{% set myDate = '2014/06/24' %}</code>).</p>
  
  <p>Sometimes it makes no difference and you can use one or the other. The <code>date()</code> filter for example works fine with both. But this isn't always the case, the <code>localeTime()</code> method is an example that exclusively takes real DateTime objects.</p>
  
  <p>Applying a <code>date</code> or <code>date_modify</code> filter to the <code>myDate</code> (string) variable is apparently not sufficient to convert it. The only solution I found is to modify <code>now</code> or fetch a DateTime object from an entry field, but I am looking for a way to generate one solely with twig in the template.</p>
  
  <p>.</p>
  
  <p><em>Edit:</em></p>
  
  <p>Thanks for your answers so far, but I still don't get this to work. For me it always results in the following template error when I use a <a href="http://buildwithcraft.com/docs/templating/datetime#methods" rel="noreferrer">DateTime method</a> on a manually composed DateTime object:</p>
  
  <blockquote>
    <p>Method "localeTime" for object "DateTime" does not exist</p>
  </blockquote>
  
  <p>Here my example code for easy reproducibility. I more and more believe this is rather a problem with those <a href="http://buildwithcraft.com/docs/templating/datetime#methods" rel="noreferrer">DateTime methods</a> and not with the way I compose the object.</p>
  
  <pre class="lang-none prettyprint-override"><code>{# Set var to string in supported format #}
  {% set myDatetimeString = '2014-06-24 23:40' %}
  
  {# Use date() function to convert string to DateTime object #}
  {% set myDatetimeObject = date(myDatetimeString) %}
  
  {# Alternatively use date_modify filter to convert string to DateTime object #}
  {% set myDatetimeObject = myDatetimeString|date_modify('+0 day') %}
  
  
  {# DateTime method doesnt work with these "objects" !! #}
  {{ myDatetimeObject.localeTime() }}
  
  {# But DateTime methods work with the `now` DateTime object #}
  {{ now.localeTime() }}
  {{ now.day() }}
  {{ now.iso8601() }}
  </code></pre>
  
Tags: <templating><datetime><variable>
Title: Is it possible to compose a DateTime object from a string?
ThreadText: # Is it possible to compose a DateTime object from a string?
  
  (Asked by: username_0 on 2014-06-24)
  
  I experienced that there is a difference between real DateTime objects (as `now` is one) and strings composed to "fake DateTime objects" (ie. `{% set myDate = '2014/06/24' %}`).
  
  Sometimes it makes no difference and you can use one or the other. The `date()` filter for example works fine with both. But this isn't always the case, the `localeTime()` method is an example that exclusively takes real DateTime objects.
  
  Applying a `date` or `date_modify` filter to the `myDate` (string) variable is apparently not sufficient to convert it. The only solution I found is to modify `now` or fetch a DateTime object from an entry field, but I am looking for a way to generate one solely with twig in the template.
  
  .
  
  *Edit:*
  
  Thanks for your answers so far, but I still don't get this to work. For me it always results in the following template error when I use a [DateTime method](http://buildwithcraft.com/docs/templating/datetime#methods) on a manually composed DateTime object:
  
  > Method "localeTime" for object "DateTime" does not exist
  
  Here my example code for easy reproducibility. I more and more believe this is rather a problem with those [DateTime methods](http://buildwithcraft.com/docs/templating/datetime#methods) and not with the way I compose the object.
  
  ```none
  {# Set var to string in supported format #}
  {% set myDatetimeString = '2014-06-24 23:40' %}
  
  {# Use date() function to convert string to DateTime object #}
  {% set myDatetimeObject = date(myDatetimeString) %}
  
  {# Alternatively use date_modify filter to convert string to DateTime object #}
  {% set myDatetimeObject = myDatetimeString|date_modify('+0 day') %}
  
  {# DateTime method doesnt work with these "objects" !! #}
  {{ myDatetimeObject.localeTime() }}
  
  {# But DateTime methods work with the `now` DateTime object #}
  {{ now.localeTime() }}
  {{ now.day() }}
  {{ now.iso8601() }}
  
  ```
  
  **Tags:** templating, datetime, variable
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-06-24. Score: 5 | Accepted answer
  
  In Twig, the `date_modify` filter returns a DateTime object. Also, `date` is not just a filter, but also a function. The `date` function accepts any a DateTime object or a string in a [supported format](http://www.php.net/manual/en/datetime.formats.php), and returns a DateTime object. Example:
  
  ```
  {{ date("+2 years") }}
  
  ```
  
  This should give you a DateTime object set for 2 years from when it's triggered.
  
  **Why is localeTime missing?**
  
  The `now` variable is not a regular DateTime object from PHP. It's actually an extended class ( \Craft\DateTime ). So it won't have all the convenience functions you get from `now` or a date field from an entry. Instead you would need to do it manually (I'm not sure if you can overwrite a function or filter in Twig). Fortunately, the locale's format string is available as a variable.
  
  ```
  {% set myDatetimeString = '2014-06-24 23:40' %}
  {% set format = craft.i18n.getTimepickerJsFormat() %}
  {{ myDatetimeString|date(format) }}
  
  ```
  
  You can even shorten it like this:
  
  ```
  {% set myDatetimeString = '2014-06-24 23:40' %}
  {{ myDatetimeString|date(craft.i18n.getTimepickerJsFormat()) }}
  
  ```
  
  To replicate localeDate, use this:
  
  ```
  {% set myDatetimeString = '2014-06-24 23:40' %}
  {{ myDatetimeString|date(craft.i18n.getDatepickerJsFormat()) }}
  
  ```
  
  > **Comments:**
  > **username_0**: These DateTime objects don't work with `localeTime()` methods for me.  
  > **username_1**: @ChristianSeelbach I gotcha now. I have edited my response to fill the missing information.  
  > **username_0**: Great username_1, thanks a lot! But that first sentence confuses me, sure you mean `now` variable?  
  > **username_1**: Yes. `now` is listed as a Global Variable in the [Docs](http://buildwithcraft.com/docs/templating/global-variables).  
  > **username_0**: username_1, does this still work for you? Tried it again today and noticed that `craft.i18n.getDatepickerJsFormat()` outputs a format like this `dd/mm/yyyy` instead of `d/m/Y`?! Which makes it useless for the date filter :(  
  > **username_1**: I haven't used much date stuff recently, but I'll be creating a simple Events section tonight for a new site. I'll check it out then.  
  
  ---
  
  ### Answer by username_2 on 2014-06-24. Score: 2
  
  This works for me, starting with a string:
  
  ```
  {% set yearbegins = '1 January ' ~ year %} // getting year from a url (dynamic route)
  {{ yearbegins | date("U") }} // outputs the date in Unixtime
  
  ```
  
  Are you saying this is not a real datetime object, and it's just the date filter making it look like it? I'm finding some unpredictability, too, as you know.
  
  > **Comments:**
  > **username_0**: In my question I already described that the `date` filter works for me to. It's the `localeTime()` method I have problems with. And in regards to that DateTime problem you encountered, I think I solved that one! See my [new answer](http://craftcms.stackexchange.com/a/447/125). ;)  
  
  ---
  
  