greduan
greduan

Reputation: 4938

Formatting post's date value in DocPad

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

Answers (2)

Tina
Tina

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

Related Questions