Action

Bujo Monthly Log

Posted by mark_boerger, Last update 20 days ago

Creates the current month with week numbers in the style of a Bullet Journal layout. Made with ChatGPT.

Steps

  • script

    // Get the current date and year
    let today = new Date();
    let year = today.getFullYear();
    let month = (today.getMonth() + 1).toString().padStart(2, '0'); // Adjust for zero-based months
    
    // Format the draft title as # YYYY-MM
    let draftTitle = `# ${year}-${month}`;
    
    // Initialize the content with the title
    let draftContent = draftTitle + "\n\n";
    
    // Get the number of days in the current month
    let daysInMonth = new Date(year, today.getMonth() + 1, 0).getDate();
    
    // Helper array for day names
    const dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    
    // Function to calculate the Sunday-based week number
    function getSundayWeekNumber(date) {
        let firstDayOfYear = new Date(date.getFullYear(), 0, 1); // January 1st
        let firstSunday = new Date(firstDayOfYear);
        
        // Find the first Sunday of the year
        firstSunday.setDate(firstSunday.getDate() + (7 - firstSunday.getDay()) % 7);
        
        // Calculate the number of weeks since the first Sunday
        let diffDays = Math.floor((date - firstSunday) / (1000 * 60 * 60 * 24));
        let weekNumber = Math.floor(diffDays / 7) + 1;  // Start from Week 1
        
        return weekNumber + 1;  // Add 1 to week number as requested
    }
    
    // Track the current week number
    let currentWeek = 0;
    
    // Build the content for each day
    for (let day = 1; day <= daysInMonth; day++) {
        let date = new Date(year, today.getMonth(), day);
        let dayOfWeek = date.getDay(); // Sunday = 0, Monday = 1, ..., Saturday = 6
    
        // Get the correct week number based on Sundays
        let weekNumber = getSundayWeekNumber(date);
    
        // If it's the first day of a new week (Sunday) or a new week detected, add the spacer and week number
        if (dayOfWeek === 0 || currentWeek !== weekNumber) {
            currentWeek = weekNumber;
            draftContent += `---\nWeek ${currentWeek}\n`;
        }
    
        // Add the day entry with a blank line afterward
        draftContent += `${day}-${dayNames[dayOfWeek]}\n\n`;
    }
    
    // Create the new draft
    let newDraft = Draft.create();
    newDraft.content = draftContent;
    newDraft.update();
    
    console.log(`Draft titled "${draftTitle}" created successfully!`);

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.