Menu
- Why Eleventy?
- Get Started
- Community
- Working with Templates
- Using Data
- Configuration
- Template Languages
- Plugins
- API Services
- Release History
- Advanced
5.81s
14.77s
Content Dates
Contents
Setting a Content Date in Front Matter Jump to heading
Add a date
key to your front matter to override the default date (file creation) and customize how the file is sorted in a collection.
---
date: 2016-01-01
---
---
date: Last Modified
---
Valid date
values:
Last Modified
: automatically resolves to the fileβs last modified dateCreated
: automatically resolves to the fileβs created date (default, this is what is used whendate
is omitted).git Last Modified
: Added in v1.0.1 automatically resolves to the fileβs latest git commit. If a file is not yet checked in to git, it assignsDate.now()
topage.date
instead.- This one is a bit resource intensive, so you may want to limit this to your CI server environment only using JavaScript data files and Environment Variables. Check out this real-world directory data file.
git Created
: Added in v2.0.0 automatically resolves to the fileβs first git commit. It uses git's--follow
flag to make a "best effort" renaming tracking. If a file is not yet checked in to git, it assignsDate.now()
topage.date
instead.- This one is a bit resource intensive, so you may want to limit this to your CI server environment only using JavaScript data files and Environment Variables. Check out this real-world directory data file.
2016-01-01
or any other valid YAML date value (leaving off the time assumes midnight in UTC, or00:00:00Z
)"2016-01-01"
or any other valid ISO 8601 string that LuxonβsDateTime.fromISO
can parse (see also the Luxon API docs).
If a date
key is omitted from the file, we then look for a YYYY-MM-DD
format anywhere in the file path (even folders). If there are multiple dates found, the first is used. βΉοΈ Note that starting in 1.0 for consistency with front matter formats file name date formats are now assumed to be UTC.
As a last resort, the file creation date is used. Careful when relying on file creation dates on a deployment server.
date
in your templates? The date
value will likely not be of much use, since Eleventy performs no transformation on this front matter value. You probably want page.date
instead. Check out the values available in the page
variable.Dates off by one day? Jump to heading
Youβre probably displaying UTC dates in a local time zone.
Many date formats in Eleventy (when set in your contentβs filename as YYYY-MM-DD-myfile.md
or in your front matter as date: YYYY-MM-DD
) assume midnight in UTC. When displaying your dates, make sure youβre using the UTC time and not your own local time zone, which may be the default.
Example Jump to heading
---
date: 2018-01-01
---
If you output the Date object in a template, it will convert it to a string for display:
Using {{ page.date }} will display a date using a local time zone like:
Sun Dec 31 2017 18:00:00 GMT-0600 (Central Standard Time)
Note that this appears to be the wrong day!
Nunjucks allows you to call JavaScript methods in output {{ page.date.toString() }}
. Liquid does not allow this.
But {{ page.date.toUTCString() }} will correctly
display a date with a UTC time zone like:
Mon, 01 Jan 2018 00:00:00 GMT
You could add your own toUTCString
filter in Liquid to perform the same task.
Also on YouTube Jump to heading
Collections out of order when you run Eleventy on your Server? Jump to heading
Be careful relying on the default date
associated with a piece of content. By default Eleventy uses file creation dates, which works fine if you run Eleventy locally but may reset in some conditions if you run Eleventy on a Continuous Integration server. Work around this by using explicit date assignments, either in your front matter or your contentβs file name. Read more at Setting a Content Date in Front Matter.
date: "git Last Modified"
feature will resolve this issue! Source control dates are available and will be consistent on most Continuous Integration servers. Read more at Setting a Content Date in Front Matter.From the Community Jump to heading
Γ27 resources courtesy of 11tybundle.dev curated by Bob Monsour