How to combine time taken for tasks?

Hiya, it’s my first post here. I’m not very experienced with dataviewjs, but this has me stumped for some weeks.

What I’m trying to do

I use obsidian to organise my day. I make daily notes with some parts in the following format, with a main task with a generic name with start and end time. These have subtasks:

## AM
- [x] 0800-0930 Wake Up
	- Coffee 
	- [x] Read
- [x] 0930-1045 Gym 
- [ ] 1115-1200 Admin
	- [ ] Emails
- [ ] Ongoing projects...
## PM
- [ ] 
## HOME
Day reflection / diary

I want to quickly look and understand how long I’m spending on the general tasks, ignoring the subtasks. For example, above should return something like “1 hour 30 minutes in Wake Up, 1 hour 15 minutes in Gym”. If the same task occurred multiple times through the day/week, it should add the values.

Things I have tried

I have been trying to make a dataviewjs script, but it seems I can’t get the note content in a straightforward manner.

The closest has been:

// DataviewJS script to calculate time spent on general tasks in daily notes
// tagged with #calendar_test, limited to the last week.

// Get the current date
const currentDate = moment();
const oneWeekAgo = currentDate.clone().subtract(7, 'days');

// Function to parse time in HHMM format and return minutes
function parseTime(time) {
    const hours = parseInt(time.substring(0, 2));
    const minutes = parseInt(time.substring(2, 4));
    return hours * 60 + minutes;
}

// Function to calculate the difference between two times
function timeDifference(startTime, endTime) {
    return parseTime(endTime) - parseTime(startTime);
}

// Filter notes tagged with #calendar_test and created within the last week
const pages = dv.pages('#calendar_test').where(p => moment(p.file.ctime).isAfter(oneWeekAgo));

// Initialize a map to hold the total time spent on each task
let taskTimeMap = {};
let debugInfo = [];  // Array to hold debug information

// Iterate over each page
pages.forEach(page => {
    // Ensure the page has content
    if (page.file.content) {
        // Extract the content of the page
        const content = page.file.content.split('\n');
        debugInfo.push(`Page: ${page.file.name}`);
        debugInfo.push("Content: " + content.join(', '));

        // Iterate over each line in the content
        content.forEach(line => {
            // Match lines that contain general tasks with start and end times
            const taskMatch = line.match(/- \[.\] (\d{4})-(\d{4}) (.+)/);
            if (taskMatch) {
                const startTime = taskMatch[1];
                const endTime = taskMatch[2];
                const taskName = taskMatch[3];
                const duration = timeDifference(startTime, endTime);

                // Add the duration to the appropriate task in the map
                if (taskTimeMap[taskName]) {
                    taskTimeMap[taskName] += duration;
                } else {
                    taskTimeMap[taskName] = duration;
                }

                debugInfo.push(`Matched Task: ${taskName}, Duration: ${duration} minutes`);
            }
        });
    } else {
        debugInfo.push(`No content in page: ${page.file.name}`);
    }
});

// Convert total minutes to hours and minutes format and display the result
dv.paragraph('Total time spent on general tasks in the last week:');
for (const [task, minutes] of Object.entries(taskTimeMap)) {
    const hours = Math.floor(minutes / 60);
    const remainingMinutes = minutes % 60;
    dv.paragraph(`**${task}:** ${hours} hours and ${remainingMinutes} minutes`);
}

// Display debug information
dv.paragraph('Debug Information:');
debugInfo.forEach(info => dv.paragraph(info));

// Display the final task time map for verification
dv.paragraph('Final Task Time Map:');
Object.entries(taskTimeMap).forEach(([task, minutes]) => {
    dv.paragraph(`${task}: ${minutes} minutes`);
});

Which returns “undefined” for either file.content or file.text . Is there a simple way to get the note content? (Maybe with a different solution). My ideal approach would be something I could see at a glance, and maybe throw into excel for plotting.

Thanks a lot for your time,
E

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.