Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration Hook/callback for custom date parsing #867

Closed
letrastudio opened this issue Jan 17, 2020 · 8 comments
Closed

Configuration Hook/callback for custom date parsing #867

letrastudio opened this issue Jan 17, 2020 · 8 comments
Labels
enhancement: favorite Vanity label! The maintainer likes this enhancement request a lot. enhancement feature: dates Related to content dates needs-documentation Documentation for this issue/feature is pending!

Comments

@letrastudio
Copy link

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

@harveyramer
Copy link

I second this. Other quick fixes seem like hacks.

@zachleat
Copy link
Member

Hmm, after seeing some pain in #822 too it does seem good to at least provide a configuration hook to add additional userland date parsing options.

@zachleat zachleat added the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label Jul 29, 2021
@zachleat zachleat changed the title Support IANA time zone codes in dates Configuration Hook/callback for custom date parsing Jul 29, 2021
@zachleat
Copy link
Member

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
Copy link
Member

zachleat commented Apr 9, 2024

Cross linking to #879 (linting data)

@uncenter
Copy link
Contributor

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!

@zachleat
Copy link
Member

Related #3315

zachleat added a commit that referenced this issue Jun 27, 2024
… return will fallback to the previous behavior. #867
zachleat added a commit that referenced this issue Jun 27, 2024
@zachleat
Copy link
Member

zachleat commented Jun 27, 2024

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 zachleat added needs-documentation Documentation for this issue/feature is pending! and removed needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. labels Jun 27, 2024
@uncenter
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: favorite Vanity label! The maintainer likes this enhancement request a lot. enhancement feature: dates Related to content dates needs-documentation Documentation for this issue/feature is pending!
Projects
None yet
Development

No branches or pull requests

4 participants