"On this day" in Obsidian
I have several years of journal entries in Obsidian, so I thought it'd be cool to have an "on this day" view in my daily notes. I think this is a standard feature in apps like Day One.
The following Dataview code has been working reliably now for several weeks, so I'll share it in case it's helpful to anyone.
if (!dv.current()) {
// The data's not available immediately on page load. If we don't handle this
// case, we'll get a big ol' stacktrace in the note.
dv.paragraph('*Waiting for data...*')
} else {
// Figure out what date substring to look for:
const dateQuery = dv.current().file.name.split(' ', 1)[0].slice(4) + ' '
// Get the substring for today's note so we can exclude it:
const thisFileDateStr = dv.current().file.name.split(' ')[0]
const thisFileDate = dv.date(thisFileDateStr)
dv.table(
['Note', 'Age'], // Table columns
dv
.pages('"Journal"') // Query notes from my "Journal" subdir
.where(
(p) =>
// Note title must include the date query and _not_ include today's date
p.file.name.includes(dateQuery) &&
!p.file.name.startsWith(thisFileDateStr)
)
.sort((p) => p.file.name, 'desc')
.map((p) => {
// Figure out the age
const age = thisFileDate.diff(
dv.date(p.file.name.split(' ')[0]),
'years'
).years
const yearsStr = age > 1 ? 'years' : 'year'
const ageStr = `${age} ${yearsStr} ago`
// Return the row values for the two columns
return [p.file.link, ageStr]
})
)
}
Note: this is JavaScript, so it needs to be wrapped in a dataviewjs
code block.
Here's what it looks like in today's note (forgive the cringey title from 2024):