Reputation: 4938
Currently if my post has date: 2013-06-16
in my post, and I do @document.date
in the post layout I get "Sat Jun 15 2013 19:00:00 GMT-0500 (CDT)".
I would like to get different formats. Like 2013-06-16
for the pubdate HTML5 tag (or something similar).
Or Jun 15, 2013
for the human readable post date, etc.
How can I accomplish this? BTW my layouts are using the .html.coffee file extension. :)
Upvotes: 1
Views: 542
Reputation: 1234
Although I love Moment.js, if you prefer a solution that does not require an additional plugin and you don't need to do too much with the date, you could use native JavaScript.
For example, if you would like to output your date in this great, human-readable format: Saturday, November 15, 2014, you can use the following native JavaScript method, passing in an optional locale:
date.toLocaleDateString()
And when you need just the year, e.g. 2014, you can use:
date.getFullYear()
Bear in mind, you will need getFullYear
and not getYear
due to the whole Y2K thing.
With all of the other native JavaScript date methods, you can make your own combinations, though if you're including multiple date formats, you may want to let Moment.js do the heavy lifting.
Upvotes: 1
Reputation: 4938
Figured it out! This is my docpad.coffee file: https://github.com/Greduan/eduantech.docpad/blob/bcc91a247e9741f4ce8aa5883634fac26c9859a5/docpad.coffee#L4-L5 https://github.com/Greduan/eduantech.docpad/blob/bcc91a247e9741f4ce8aa5883634fac26c9859a5/docpad.coffee#L41-L43
And here's my post template: https://github.com/Greduan/eduantech.docpad/blob/bcc91a247e9741f4ce8aa5883634fac26c9859a5/src/layouts/post.html.coffee#L7-L8
Of course I only linked the relevant parts of the code. Basically I learned and used Moment.js. :)
Upvotes: 3