Building on Hours worked for week

What I’m trying to do

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)

I would like the week to be calculated starting on Monday and ending on Sunday.

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