How to calculate my work hours

First, Thank you AlanG.

I made the following changes to only display the current week and handle roll-over time into the next day. (For those that work late hours)

const tracked = {}
dv.pages('"daily/2023"').file.lists
  .where(x => x.section.subpath === "Worklog").array()
  .forEach(x => {
    // /(\d{4}\d{2}\d{2})/ - Works too
    const date = x.path.match(/(\d{8})/)[1]
    const week = moment(date).format('YYYY, [Week] WW')
	if (week == moment().format('YYYY, [Week] WW')) {

        // Find the start/end times for each bullet point
        const times = x.text.match(/^(\d{2}:\d{2}) - (\d{2}:\d{2})/)
        if (times) {
            const start = moment(date + times[1], 'YYYYMMDDHH:mm')
            var end = moment(date + times[2], 'YYYYMMDDHH:mm')
            if ( start > end) {
               end = moment(date + times[2], 'YYYYMMDDHH:mm').add(1, 'days')
            } 
            
	        const minutes = moment.duration(end.diff(start)).asMinutes()

	        // Setup the week
            if (!tracked[week]) tracked[week] = {}

	        // Add up the date
            if (tracked[week][date]) {
	            tracked[week][date].minutes += minutes
	        } else {
		        tracked[week][date] = {
		            path: x.path,
		            minutes: minutes
		        }
		    }
	    }
	}
})

const hours = minutes => (minutes / 60).toFixed(1)

const table = []
Object.keys(tracked).sort((a, b) => b.localeCompare(a))
  .forEach(weekDate => {
    // Push weekly value
    const week = tracked[weekDate]
    const weekTime = Object.values(week).reduce((prev, curr) => prev + curr.minutes, 0)
    table.push([weekDate, '**' + hours(weekTime) + '**'])

    // Push daily values
    Object.keys(week).sort((a, b) => b.localeCompare(a))
      .forEach(date => {
        const link = `– [[${week[date].path}#Worklog|${moment(date).format('dddd, D MMMM')}]]`
        table.push([link, '– ' + hours(week[date].minutes)])
      })
  })

dv.table(['Date', 'Hours'], table)
1 Like