Habit Calendar

These are the code blocks I used in the related video. You can copy them from here and adapt them as needed or just download the fully functional Lean Habits Vault.

Dont forget!

To put the code blocks between triple tickmarks (```).

Intermittent Fasting (yes/no) - current month

dataviewjs
// Get the current date
const currentDate = new Date();

// Extract the current year from the date
const currentYear = currentDate.getFullYear();

// Extract the current month from the date and adjust for JavaScript's 0-indexed months
const currentMonth = currentDate.getMonth() + 1; // JavaScript months are 0-indexed (0-11)

// Execute a Dataview query to create a table
const table = await dv.query(`
TABLE choice(Challenge1,"🟢","🔴") AS "🍴"    
FROM "06 BJ/10 Daily"
`);

// Log the table to the console for debugging purposes
console.log(table);

// Render a habit calendar in the container with the current year and month
renderHabitCalendar(this.container, dv, {
    year: currentYear,
    month: currentMonth,
    data: table
});

All-in-one - current month

dataviewjs
// Get the current date
const currentDate = new Date();

// Extract the current year from the date
const currentYear = currentDate.getFullYear();

// Extract the current month from the date and adjust for JavaScript's 0-indexed months
const currentMonth = currentDate.getMonth() + 1; // JavaScript months are 0-indexed (0-11)

// Execute a Dataview query to create a table with various habit tracking fields
const table = await dv.query(`
TABLE choice(Walk,"🟢","🔴") AS "🚶",
    choice(Workout,"🟢","🔴") AS "🏋️‍♀️",
    choice(Challenge1,"🟢","🔴") AS "🍴",
    Sleep AS "😴",
    Challenge2 AS "PU",
    Challenge3 AS "CR"
FROM "06 BJ/10 Daily"
`);

// Log the table to the console for debugging purposes
console.log(table);

// Render a habit calendar in the container with the current year and month
renderHabitCalendar(this.container, dv, {
    year: currentYear,
    month: currentMonth,
    data: table
});

All-in-one - previous month

dataviewjs
// Get the current date
const currentDate = new Date();

// Calculate the first date of the previous month
const previousMonthDate = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 1);

// Extract the year of the previous month
const previousYear = previousMonthDate.getFullYear();

// Extract the month of the previous month and adjust for JavaScript's 0-indexed months
const previousMonth = previousMonthDate.getMonth() + 1; // Adjust for 0-indexed months

// Execute a Dataview query to create a table with various habit tracking fields
const table = await dv.query(`
TABLE choice(Walk,"🟢","🔴") AS "🚶",
    choice(Workout,"🟢","🔴") AS "🏋️‍♀️",
    choice(Challenge1,"🟢","🔴") AS "🍴",
    Sleep AS "😴",
    Challenge2 AS "PU",
    Challenge3 AS "CR"
FROM "06 BJ/10 Daily"
`);

// Log the table to the console for debugging purposes
console.log(table);

// Render a habit calendar in the container with the previous year and month
renderHabitCalendar(this.container, dv, {
    year: previousYear,
    month: previousMonth,
    data: table
});