Skip to content

Instantly share code, notes, and snippets.

@RuizSerra
Created May 31, 2024 06:53
Show Gist options
  • Save RuizSerra/db7ecc12a95d243188d90bc56ee93650 to your computer and use it in GitHub Desktop.
Save RuizSerra/db7ecc12a95d243188d90bc56ee93650 to your computer and use it in GitHub Desktop.
/*
Given an Obsidian markdown note named "YYYYwNN.md",
where YYYY is the year and NN is the week number,
e.g. "2024w07.md", this Dataview JS snippet will list all
the notes in the valut created between the Thursday of that week
and the Wednesday of the following week.
Include this code snippet in your weekly note template within a dataviewjs block:
```dataviewjs
this code here...
```
*/
// Get the current file name
const fileName = dv.current().file.name;
// Extract the year and week from the file name
const year = parseInt(fileName.slice(0, 4));
const week = parseInt(fileName.slice(5));
// Function to get the start date of a given week number
function getStartOfWeek(year, week) {
const firstDayOfYear = new Date(year, 0, 1);
const daysOffset = (week - 1) * 7;
const firstMondayOfYear = firstDayOfYear.getDay() === 1 ? firstDayOfYear : new Date(firstDayOfYear.setDate(firstDayOfYear.getDate() + (8 - firstDayOfYear.getDay())));
return new Date(firstMondayOfYear.setDate(firstMondayOfYear.getDate() + daysOffset));
}
// Get the start date of the week
const startDateOfWeek = getStartOfWeek(year, week);
// Calculate the start date (Thursday of the given week)
const startDate = new Date(startDateOfWeek);
startDate.setDate(startDateOfWeek.getDate() + 4); // Move to Thursday
// Calculate the end date (Wednesday of the following week)
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + 7); // End date is Wednesday of the following week
// Convert dates to ISO format strings for the Dataview query
const startDateString = startDate.toISOString().split("T")[0];
const endDateString = endDate.toISOString().split("T")[0];
// Dataview query
dv.table(["File Name", "Created Time"],
dv.pages("")
.where(p => p.file.ctime >= dv.date(startDateString) && p.file.ctime <= dv.date(endDateString))
.sort(p => p.file.ctime, 'asc')
.map(p => ["[[" + p.file.name + "]]", new Date(p.file.ctime).toLocaleString('en-GB', { weekday: 'short', day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit' })])
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment