<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>

CC BY-SA 3.0
2015-12-14T09:16:20.237Z
So is ```$jsDate = $entry->postDate.iso8601();``` happening in the ```config/elementapi.php``` then?
Steven Grant
CC BY-SA 3.0
2015-12-14T19:44:10.183Z
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.
RitterKnight
CC BY-SA 3.0
2015-12-15T16:10:46.023Z
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()`
Michael Rog
CC BY-SA 3.0
2015-12-15T16:14:55.063Z
Oops, duh- fixed, thank you, Your answer is much better than mine!
RitterKnight
CC BY-SA 3.0
2015-12-12T17:48:21.397Z
2015-12-15T16:15:04.080Z
2015-12-15T16:15:04.080Z
RitterKnight
<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>

CC BY-SA 3.0
2015-12-16T14:12:07.793Z
As an FYI - need to do 'postDate' => $entry->postDate->getTimestamp()*1000 as Angular needs the timestamp in milliseconds
Steven Grant
CC BY-SA 3.0
2015-12-15T16:06:39.930Z
2015-12-17T18:40:43.213Z
2015-12-17T18:40:43.213Z
Michael Rog
<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>

<json><plugin-elementapi>
Formatting postDate using the ElementAPI plugin
# 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 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, 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!  

---

