Calculation of total work hours with dataview in Inline field

What I’m trying to do

I am trying to calculate the total worktime hours in my daily notes, so that I could use dataview TABLE in a weekly overview to show this calculation.
Since I have multiple irregular breaks during the day I want to write down every work session’s start and end time in a format HH:MM and then calculate the total time.

Things I have tried

This is the code I have tried:

w1_in:: 12:30
w1_out:: 13:31
w2_in:: 14:05
w2_out:: 16:52

work1:: =date( + "T" + this.w1_out) - date( + "T" + this.w1_in)
type - =typeof(this.work1)
work2:: =date( + "T" + this.w2_out) - date( + "T" + this.w2_in)
type - =typeof(this.work2)

total work time:: =dur(this.work1) + dur(this.work2)

I thought that dur() function accepts any data type, but apparently it cant calculate this weird string which is “work1” and “work2”
This is the output:

Note: I have my daily note format as YYYY-MM-DD.

I don’t think you’ll be able to use the value of another calculated inline field for your total sum.

It’s lengthy, but doing all the calculations within the final inline field may be the only option.

`= dur(date( + "T" + this.w1_out) - date( + "T" + this.w1_in)) + dur(date( + "T" + this.w2_out) - date( + "T" + this.w2_in))`
1 Like

Thank you! This solves the issue.
I have literally tried everything but this simple solution.
I actually don’t mind the lengthiness of the field, most importantly - it works.

Just a quick note for those who are doing this silly thing here.
If you have setup inline fields (in your daily template) for some number of work sessions, and if you don’t fill out all of them, then it gives you an error saying it can’t calculate null + duration.
It this case you can use default() function to replace null with 0s duration. Here is the code for 5 work sessions:

W1 start:: 10:00
W1 end:: 11:00

W2 start:: 12:00
W2 end:: 13:00

W3 start:: 14:00
W3 end:: 15:00

W4 start:: 16:00
W4 end:: 17:00

W5 start::
W5 end::

Total work time:: = default(dur(date( + "T" + this.w1-end) - date( + "T" + this.w1-start)), dur(0s)) + default(dur(date( + "T" + this.w2-end) - date( + "T" + this.w2-start)), dur(0s)) + default(dur(date( + "T" + this.w3-end) - date( + "T" + this.w3-start)), dur(0s)) + default(dur(date( + "T" + this.w4-end) - date( + "T" + this.w4-start)), dur(0s)) + default(dur(date( + "T" + this.w5-end) - date( + "T" + this.w5-start)), dur(0s))

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