Skip to content

Instantly share code, notes, and snippets.

@zudochkin
Created January 3, 2024 10:49
Show Gist options
  • Save zudochkin/f73b81e0a511ec68aee9a6a1c7de3f6b to your computer and use it in GitHub Desktop.
Save zudochkin/f73b81e0a511ec68aee9a6a1c7de3f6b to your computer and use it in GitHub Desktop.
Calendar Builder for Obsidian
// get DailyNotes template settings for weekly and daily notes
const periodicNotes = app.plugins.plugins["periodic-notes"];
const periodicNotesSetings = periodicNotes.settings;
function mdCalendar(year, month, mondayFirst) {
if (mondayFirst === undefined) mondayFirst = false;
//Step 1: build an array of the abbreviated weekday names
let dd = new Date(2022, 1, 27); //a Sunday
let wnames = [];
for (let i = 0; i < 8; i++) {
wnames.push(dd.toLocaleString("default", { weekday: "short" }));
dd.setDate(dd.getDate() + 1);
}
if (mondayFirst) {
wnames = wnames.slice(1, 8); //gives [Mon,Tue,Wed,Thu,Fri,Sat,Sun]
} else {
wnames = wnames.slice(0, 7); //gives [Sun,Mon,Tue,Wed,Thu,Fri,Sat]
}
//Step 2: Get first day of the month
// (Note: in the javascript Date object, the month has values from 0[Jan] to 11[Dec].)
let day = new Date(year, month - 1, 1);
//Step 3: Establish the calendar header which includes the month, year, and abbreviated weekday names
let cal =
`${day.toLocaleString("default", { month: "long" })} ${year}\n\n` +
`| Week | ${wnames.join(" | ")} |\n` +
"|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|\n";
//Step 4: Populate the calendar with the days of the month
let week = [
" .. ",
" .. ",
" .. ",
" .. ",
" .. ",
" .. ",
" .. ",
];
while (day.getMonth() == month - 1) {
let wday = day.getDay(); //day of the week (0[Sun] - 6[Sat])
momentDay = moment(day);
let formattedDay = momentDay.format(periodicNotesSetings.daily.format)
let formattedWeek = momentDay.format(periodicNotesSetings.weekly.format)
if (mondayFirst) wday = wday - 1 < 0 ? 6 : wday - 1;
if (day.getDate() === 1 || wday === 0) {
cal +=
"|" +
`[[${formattedWeek}\\|${momentDay.format("WW")}]]`;
}
let d = `${day.getDate()}`;
//week[wday] = ' ' + d.padStart(2,'0') + ' ';
//week[wday] = ' [[' + day.toISOString().slice(0,10) + '\\|' + d.padStart(2,'0') + ']] ';
week[wday] = `[[${formattedDay}\\|${momentDay.format(
"DD",
)}]]`;
if (wday == 6) {
cal += "|" + week.join("|") + "|\n";
week = [
" .. ",
" .. ",
" .. ",
" .. ",
" .. ",
" .. ",
" .. ",
];
}
day = new Date(day.getFullYear(), day.getMonth(), day.getDate() + 1);
}
if (week[0] != " .. ") cal += "|" + week.join("|") + "|\n";
return cal;
}
module.exports = mdCalendar;
@zudochkin
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment