eleventy
eleventy copied to clipboard
Configuration Hook/callback for custom `date` parsing
I'd like to use IANA time zone codes for specifying localized time in post dates, like so:
date: 2019-08-31 23:59:56 Europe/Lisbon
In my humble opinion this would be the best way to avoid the dates off by one day pitfall.
Unlike UTC-based time offsets (e.g. +01:00), IANA codes abstract away offset differences from Daylight Saving Time, so a single value magically works all year round. They're also easier to understand, and provide a bit more geographical context, which is a nice bonus.
IANA codes are supported in Luxon, represented by the z token.
Eleventy currently rejects this date format as invalid, but it works in Jekyll. I'm porting over my Jekyll blog, and with over 800 posts all using this date format, um, help
I second this. Other quick fixes seem like hacks.
Hmm, after seeing some pain in https://github.com/11ty/eleventy/issues/822 too it does seem good to at least provide a configuration hook to add additional userland date parsing options.
This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.
View the enhancement backlog here. Don’t forget to upvote the top comment with 👍!
Cross linking to #879 (linting data)
I'd love to see this change! For some time I've thought about removing the custom date parsing entirely and suggesting a way to implement it in the documentation, but seeing as this feature is pretty popular "removing" the default functionality could easily be done with this kind of hook (just return the date string with no processing) and obviously allows people to have their own date formats too!
Related #3315
v3.0.0-alpha.15 will include an eleventyConfig.addDateParsing method for adding your own custom date parsing logic. This is a preprocessing step for existing Date logic.
You can return:
- a Luxon
DateTimeinstance to short-circuitpage.datewith this new value (we do the.toJSDate()conversion for you). - a JavaScript
Dateto short-circuitpage.datewith this new value. - any new valid value for
datewill be processed using existing logic: https://www.11ty.dev/docs/dates/ As an example, you can return a new string that will be processed by Luxon. - anything falsy (or no
return) will skip any assignment from the callback.
Here’s an example using IANA time zone codes from the Eleventy test suite.
---
date: 2019-08-31 23:59:56 America/New_York
---
# Markdown
import { DateTime } from "luxon";
export default function(eleventyConfig) {
eleventyConfig.addDateParsing(function(dateValue) {
return DateTime.fromFormat(dateValue, "yyyy-MM-dd hh:mm:ss z");
});
};
You can add multiple callbacks and chain them together and they’ll all run before the fallback logic takes hold of the new value:
eleventyConfig.addDateParsing((rawDateValueFromTemplate) => rawDateValueFromTemplate);
eleventyConfig.addDateParsing((rawDateValueFromFirstCallback) => rawDateValueFromFirstCallback);
Wow that's amazing, thanks Zach! Exactly what I was looking for.
Preview docs deploying here: https://11ty-website-git-v3-11ty.vercel.app/docs/dates/#configuration-api-for-custom-date-parsing