Skip to content

Instantly share code, notes, and snippets.

@charliepark
Created March 28, 2020 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charliepark/67ae361f768f7c48c26c01513170aea3 to your computer and use it in GitHub Desktop.
Save charliepark/67ae361f768f7c48c26c01513170aea3 to your computer and use it in GitHub Desktop.
Use the HTML5 <time> element while nicely formatting your blog post's date
// in .eleventy.js, add the two functions inside your `module.exports = function (eleventyConfig) {}` block:
```
eleventyConfig.addFilter("toISOString", (date) => (
new Date(`${date} 00:00:00`).toISOString()
));
eleventyConfig.addFilter("formatDateForBlog", (date) => (
new Date(`${date} 00:00:00`).toLocaleString('en-us', { month: 'long', day: 'numeric', year: 'numeric' })
));
```
// in your layout/template file, call them like this:
```
<time datetime="{{ date | toISOString }}">{{ date | formatDateForBlog }}</time>
```
// the rendered HTML will look like this:
```
<time datetime="2020-03-27T07:00:00.000Z">March 27, 2020</time>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment