Answers.Body: <p>Anything you spit out of Craft is going to be a string until you convert it into javascript so it's "native" to Angular/Javascript. JSON is just a transport format. There's no <a href="http://weblog.west-wind.com/posts/2014/Jan/06/JavaScript-JSON-Date-Parsing-and-real-Dates" rel="nofollow">standard format</a> for dates in JSON (which is a little counter intuitive!).</p>
  
  <p>I would do something like:</p>
  
  <pre><code>$jsDate =  $entry-&gt;postDate-&gt;iso8601();
  ... // output as usual
  </code></pre>
  
  <p>8601 is a good format to get your date into that JS can convert. Then make that into a JS <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" rel="nofollow">Date</a>, something like:</p>
  
  <pre><code>var angularDate = new Date(whatever);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T09:16:20.237Z
Answers.Text: So is ```$jsDate = $entry->postDate.iso8601();``` happening in the ```config/elementapi.php``` then?
Answers.UserDisplayName: Steven Grant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T19:44:10.183Z
Answers.Text: iso8601 a helper [method](https://craftcms.com/classreference/etc/dates/DateTime#iso8601-detail) in the DateTime class.  Since the Element API just extends Craft, anything you can call in Craft is fair game. ``postDate`` just happens to be a DateTime object which is why it works.
Answers.UserDisplayName: RitterKnight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-15T16:10:46.023Z
Answers.Text: I don't think the dot accessor syntax will work in the context of the JSON transformer. The dot syntax is for Twig, whereas in PHP you'd want to use the arrow syntax, like `$jsDate =  $entry->postDate->iso8601()`
Answers.UserDisplayName: Michael Rog
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-15T16:14:55.063Z
Answers.Text: Oops, duh- fixed, thank you, Your answer is much better than mine!
Answers.UserDisplayName: RitterKnight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-12T17:48:21.397Z
Answers.LastActivityDate: 2015-12-15T16:15:04.080Z
Answers.LastEditDate: 2015-12-15T16:15:04.080Z
Answers.UserDisplayName: RitterKnight
Answers.Body: <p><code>$entry-&gt;postDate</code> will give you the <code>postDate</code> property of the <code>EntryModel</code>, which itself is a <code>DateTime</code> object.</p>
  
  <p>The JSON transformer will do its best to convert that object to JSON by returning its visible properties, which produces the JSON object in your example.</p>
  
  <p>Angular has built-in date filters, but it requires the input data to be in a recognized format. So, what you really want in your JSON - rather than a representation of the whole <code>DateTime</code> object - is a more primitive value that Angular knows how to interpret as a date: You need a <em>timestamp</em>.</p>
  
  <p>Luckily, you can easily access the timestamp of the PHP <code>DateTime</code> object in your transformer, like this:</p>
  
  <pre><code>'postDate' =&gt; $entry-&gt;postDate-&gt;getTimestamp() * 1000
  </code></pre>
  
  <p><em>(We're multiplying the timestamp by 1000 because Angular wants the timestamp to represent milliseconds; PHP gives us the timestamp in seconds.)</em></p>
  
  <p>Then, in Angular you can use the date filter to change the format as desired, which in your front-end code would look something like: <code>{{ note.postDate | date:'medium' }}</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-16T14:12:07.793Z
Answers.Text: As an FYI - need to do 'postDate' => $entry->postDate->getTimestamp()*1000 as Angular needs the timestamp in milliseconds
Answers.UserDisplayName: Steven Grant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-15T16:06:39.930Z
Answers.LastActivityDate: 2015-12-17T18:40:43.213Z
Answers.LastEditDate: 2015-12-17T18:40:43.213Z
Answers.UserDisplayName: Michael Rog
Body: <p>Building a small AngularJS app and using the ElementAPI plugin for the first time.</p>
  
  <p>I can set <code>'postDate' =&gt; $entry-&gt;postDate</code> which gives me</p>
  
  <pre><code>postDate : {
     date: "2015-12-10 07:04:02.000000",
     timezone_type: 3,
     timezone: "UTC"
  }
  </code></pre>
  
  <p>as an example.</p>
  
  <p>Now I can access this date in Angular by doing <code>{{note.postDate.date}}</code> but obviously it's just a string so I can't use any native Angular filters for formatting.</p>
  
  <p>Is there a way I can access the postDate with a date format? What options do I have? </p>
  
Tags: <json><plugin-elementapi>
Title: Formatting postDate using the ElementAPI plugin
ThreadText: # Formatting postDate using the ElementAPI plugin
  
  (Asked by: username_0 on 2015-12-12)
  
  Building a small AngularJS app and using the ElementAPI plugin for the first time.
  
  I can set `'postDate' => $entry->postDate` which gives me
  
  ```
  postDate : {
     date: "2015-12-10 07:04:02.000000",
     timezone_type: 3,
     timezone: "UTC"
  }
  
  ```
  
  as an example.
  
  Now I can access this date in Angular by doing `{{note.postDate.date}}` but obviously it's just a string so I can't use any native Angular filters for formatting.
  
  Is there a way I can access the postDate with a date format? What options do I have?
  
  **Tags:** json, plugin-elementapi
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-12-15. Score: 7 | Accepted answer
  
  `$entry->postDate` will give you the `postDate` property of the `EntryModel`, which itself is a `DateTime` object.
  
  The JSON transformer will do its best to convert that object to JSON by returning its visible properties, which produces the JSON object in your example.
  
  Angular has built-in date filters, but it requires the input data to be in a recognized format. So, what you really want in your JSON - rather than a representation of the whole `DateTime` object - is a more primitive value that Angular knows how to interpret as a date: You need a *timestamp*.
  
  Luckily, you can easily access the timestamp of the PHP `DateTime` object in your transformer, like this:
  
  ```
  'postDate' => $entry->postDate->getTimestamp() * 1000
  
  ```
  
  *(We're multiplying the timestamp by 1000 because Angular wants the timestamp to represent milliseconds; PHP gives us the timestamp in seconds.)*
  
  Then, in Angular you can use the date filter to change the format as desired, which in your front-end code would look something like: `{{ note.postDate | date:'medium' }}`
  
  > **Comments:**
  > **username_0**: As an FYI - need to do 'postDate' => $entry->postDate->getTimestamp()*1000 as Angular needs the timestamp in milliseconds  
  
  ---
  
  ### Answer by username_1 on 2015-12-12. Score: 2
  
  Anything you spit out of Craft is going to be a string until you convert it into javascript so it's "native" to Angular/Javascript. JSON is just a transport format. There's no [standard format](http://weblog.west-wind.com/posts/2014/Jan/06/JavaScript-JSON-Date-Parsing-and-real-Dates) for dates in JSON (which is a little counter intuitive!).
  
  I would do something like:
  
  ```
  $jsDate =  $entry->postDate->iso8601();
  ... // output as usual
  
  ```
  
  8601 is a good format to get your date into that JS can convert. Then make that into a JS [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), something like:
  
  ```
  var angularDate = new Date(whatever);
  
  ```
  
  > **Comments:**
  > **username_0**: So is ```$jsDate = $entry->postDate.iso8601();``` happening in the ```config/elementapi.php``` then?  
  > **username_1**: iso8601 a helper [method](https://craftcms.com/classreference/etc/dates/DateTime#iso8601-detail) in the DateTime class.  Since the Element API just extends Craft, anything you can call in Craft is fair game. ``postDate`` just happens to be a DateTime object which is why it works.  
  > **username_2**: I don't think the dot accessor syntax will work in the context of the JSON transformer. The dot syntax is for Twig, whereas in PHP you'd want to use the arrow syntax, like `$jsDate =  $entry->postDate->iso8601()`  
  > **username_1**: Oops, duh- fixed, thank you, Your answer is much better than mine!  
  
  ---
  
  