Add Date Filters
I wanted to make a blog, but displaying dates correctly in Eleventy is a common pitfall. To get around this, I used date filters. The first filter will make the date display correctly in ISO format (e.g. YYYY-MM-DD). The second filter will make the date display with the month spelled out (e.g. YYYY Month DD). I made the date filters first, and then the blog.
- In
.eleventy.js, abovemodule.exportsaddedconst { DateTime } = require("luxon"); - In
.eleventy.js, belowmodule.exportsbut abovereturn {added:
// Filters
eleventyConfig.addFilter("correctISO", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc"}).toFormat("yyyy-MM-dd");
});
eleventyConfig.addFilter("niceDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc"}).toFormat("dd MMMM yyyy");
});