Created
May 17, 2021 14:19
-
-
Save yaserahmady/fc2819eb89530a79eb6da02e09d6c507 to your computer and use it in GitHub Desktop.
"On this day" feature for Obsidian DataviewJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dates = { | |
today: new Date(), | |
activeFile: new Date(app.workspace.getActiveFile().name.substring(0, 10)), | |
}; | |
let dateToCompare = dates.today; | |
let openFileDateFormatted = formatDate(dates.activeFile); | |
let pages = dv.pages('"Journal"').where(onThisDay); | |
let columns = ["File"]; | |
function render() { | |
dates.activeFile = new Date( | |
app.workspace.getActiveFile().name.substring(0, 10) | |
); | |
if (isNaN(dates.activeFile)) { | |
dateToCompare = dates.today; | |
} else { | |
dateToCompare = dates.activeFile; | |
} | |
pages = dv.pages('"Journal"').where(onThisDay); | |
dv.container.empty(); | |
dv.header(6, "On this day"); | |
dv.list(pages.file.link); | |
} | |
render(); | |
app.workspace.on("file-open", function cb() { | |
render(); | |
}); | |
function pad(n, width, z) { | |
z = z || "0"; | |
n = n + ""; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
} | |
function formatDataviewDate(dateObject) { | |
let month = pad(dateObject.file.day.month, 2); | |
let day = pad(dateObject.file.day.day, 2); | |
return `-${month}-${day}`; | |
} | |
function formatDate(date) { | |
let month = pad(date.getMonth() + 1, 2); | |
let day = pad(date.getDate(), 2); | |
return `-${month}-${day}`; | |
} | |
function onThisDay(dailyNote) { | |
return formatDataviewDate(dailyNote) == formatDate(dateToCompare); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment