eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Configuration Hook/callback for custom `date` parsing

Open letrastudio opened this issue 5 years ago • 4 comments

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

letrastudio avatar Jan 17 '20 00:01 letrastudio

I second this. Other quick fixes seem like hacks.

harveyramer avatar Jul 25 '20 15:07 harveyramer

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.

zachleat avatar Jul 29 '21 14:07 zachleat

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 👍!

zachleat avatar Jul 29 '21 14:07 zachleat

Cross linking to #879 (linting data)

zachleat avatar Apr 09 '24 17:04 zachleat

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!

uncenter avatar Jun 16 '24 15:06 uncenter

Related #3315

zachleat avatar Jun 19 '24 15:06 zachleat

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 DateTime instance to short-circuit page.date with this new value (we do the .toJSDate() conversion for you).
  • a JavaScript Date to short-circuit page.date with this new value.
  • any new valid value for date will 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);

zachleat avatar Jun 27 '24 20:06 zachleat

Wow that's amazing, thanks Zach! Exactly what I was looking for.

uncenter avatar Jun 27 '24 21:06 uncenter

Preview docs deploying here: https://11ty-website-git-v3-11ty.vercel.app/docs/dates/#configuration-api-for-custom-date-parsing

zachleat avatar Sep 25 '24 21:09 zachleat