Skip to content

Published on this day

Published on .

I previously wrote about how to list related files in Obsidian. The code described there will pull any file from anywhere as long as the filename matches the current date. This works perfectly well in my work Vault because of the file naming convention I have there.

As I have mentioned before, I write my blog posts in Obsidian too. I had this idea of surfacing any posts I had written for that day in my Day Notes. It will be nice to track what I have been writing about or just to reminisce. It's a trickier ask than my previous code because the filename can be anything — I really need to be looking at the published date.

My YAML frontmatter lists the published date like this;

date: 2024-02-21T10:06:53+00:00

My Day Notes use the ISO 8601 date format standard for the filename.

So, because it's not a like-for-like formatting, we need to do some formatting!

Ideally we want the comparison to be;

// Pseudo-code
WHERE publishedDate('MM-DD') === dayNote.filename('MM-DD')

Luckily, DataView offers some powerful functionality — with a few "gotchas"!

Here is the final code for your dataview codeblock;

TABLE without ID dateformat(date, "yyyy") as Year, file.name as Post
WHERE file.folder = "thomasrigby.com/Published"
AND date != null
AND dateformat(date, "yyyy") <= dateformat(date(this.file.name), "yyyy")
AND dateformat(date, "MM-dd") = dateformat(date(this.file.name), "MM-dd")
SORT date DESC

Let's break that down!

We will use TABLE without ID as we're not interested in linking to the file in Obsidian.

Following that we define our table columns; "Year" will use the published date formatted into just the year. "Post" shows the file name as it shows in the Obsidian sidebar.

We restrict the search to the "thomasrigby.com/Published" file.folder as this is specifically related to published articles on my website - obviously this will be different for you!

Check the date value exists then we're onto the most important bit…

dateformat(date, "MM-dd") = dateformat(date(this.file.name), "MM-dd") is the DataView way of writing the pseudo-code above. As I said, there's a couple of "gotchas" here.

  1. this.file.name is a String so it needs to be converted to use dateformat which is why it has the (clunky) dateformat(date()) syntax.
  2. The output of dateformat is a String, not a Date object.
  3. The dateformat function uses Luxon tokens which are different to JavaScript and Moment.js; notably the yyyy and dd instead of YYYY and DD which caused me all kinds of headaches until I figured it out!

We're also checking the article has been published that year (yyyy) or earlier. This will prevent seeing 2024 posts on 2015 Day Notes.

We finish by SORTing the entries by published date with the most recent first.

Now I can see what I published on my blog when I check my Day Notes each morning. Maybe it's an opportunity to revisit an article if I've changed my opinion or the information is out of date.

I hope this is useful to someone else! 😎

Edit 2024-02-22

  • Updated file.name to this.file.name so it actually works.
  • Added the "this year or older" conditional.

Fin

Comments

In almost all cases, the comments section is a vile cesspool of Reply Guys, racists, and bots.

I don't want to have to deal with that kind of hell so I don't have a comments section.

If you want to continue the conversation, you can always hit me up on the Socials™ (which is not a vile cesspool of Reply Guys, racists, and bots).