Change date format in front matter

I have hundreds of files that do not have published date on the front matter but use the date appended to the file name. I also have an updated date on the front matter, but in the format of Nov 01, 2021 e,g updated: Nov 01, 2021. I would like to convert all these dates to the format of 2021-10-01 in all files. Is it possible with Jekyll or will I have to do this manually?.

1 Like

jekyll doesn’t have any way to do that - if you mean something that would actually change that on the file. I think it could take that date in and output as a different format when rendering.

I’ve seen command line scripts that could loop thru files and do interesting things like that, though not sure changing the date inside a yaml variable would be doable - probably is though. No idea how to do it though.

To rename the files you would need some shell scripting or a Python script.

Happy to help with this, if you can supply a couple of filenames and metadata in those, along with the expected result.

E.g.

Input:

Filename abc-…2011.md
Frontmatter:

...

Expected output result:
Filename abc-…2011-01-02.md
Frontmatter:

date: 2011-01-02

Or maybe you want to remove the date from the filename? And then have to Frontmatter fields as dates, like created and updated.


You can use Jekyll to read the names of your files and process them, without having to actually rename and files.

You could use

page.path

Or maybe page.name

And that would be my-title-nov-01-2011.md maybe

And you split that by - then get the last items. Excluding the extension too.

Then you will have year, month, and day as separate variables and you could make a new variable from them to sort by or output.

It can get messy as a Jekyll is not elegant for this kind of thing.

Plus you’ll have to duplicate the logic if you want to apply it on a page itself and a list of pages and wherever the page gets used.

Compared to doing the work to get the data set on each page the way you want and then referencing the formatted value.

It’s also more predictable to work with formatted values as what the logic is done what you see is what you get. No imagining how the date will be interpreted and if it is correct, when adding a new page.

For a very simple case where you do have frontmatter, I’d recommend VS Code find and replace the contents of the files.

It supports regex patterns.

You can preview the result of the substitution.

And you can apply one at a time or in a batch.


open the search pane.

Enable the regex icon.

Enter a pattern like this in Find

date: (\w+) (\d\d), (\d\d\d\d)

To match Nov 01, 2021.

Then enter Replace as

date: $3-$1-$2

To get 2021-Nov-01

Each of the groups in brackets becomes numbered as 1 (month), 2 (day), and 3 (yeae). So I have reordered them such that the 3rd group for the year now appears first.

Click in the text field to select it.

and press enter do to a search.

See the preview of what will be replaced.

Click Replace All when happy.


That is half the job.

I left month as words.

You can do a second pass to fix your months.

Find:

date: (\d\d\d\d)-Jan-(\d\d)

Maybe escape dash as \- ?

Now year is $1 and day is $2 and the middle is January.

So Replace as

date: (\d\d\d\d)-01-(\d\d)

Then Replace All.

And repeat for each month so 12 times.

1 Like

@MichaelCurrin This is a great solution! The only thing I am noticing is if someone were to have a date like “Oct 1, 2021” this would not work, because the month does not have a zero (0) in front of it. I am not a regex expert, but it would be cool if you know how to modify the search to account for that, others might find it helpful.

Regex101.com is great for testing out rules against cases and checking syntax

And in the bottom right you can search for syntax. I like that as a cheatsheet or learning reference.

Make character optional:

\d?\d

e.g.

Matches on both b and ab as literal characters.

a?b

Match on optional s and optional group www

https?:\/\/(www\.)?

Alternative

One or two repeats of prev character

\d{1,2}

See Repeat and Quantifiers

Cool, thanks! I know this was not my question, but that was super helpful!

1 Like