Why moment/duration cannot identify time duration in YAML

I’m trying to use dataview to track total time of video watching. In each files YAML I have:
length: hh:mm:ss

I want to transform that into a time duration, then add them up. I tested in that page with the following line:

moment.duration(this.length).asMinutes()

But it returns 0. I tested with the following code:

moment.duration(‘00:23:19’).asMinutes()

and it works just fine.

Things I have tried

I tried the following querys and none of them work:

moment.duration(‘this.length’).asMinutes()
moment.duration(‘this.length’.toString()).asMinutes()

I also tried to modify my YAML into:
hr: hh
min: mm
sec: ss

and then use:

moment.duration({hours:hh, minutes:min, seconds:ss}).asMinutes()

and it still returns 0.

What I’m trying to do

I don’t know really. I’ve never learned JS before.

Topic

Summary
  • How to transform a time interval like hh:mm:ss into a time duration and sum a duration in multiple pages?

Test

Summary
  • dataview: v0.5.46

Input

Summary

dictionary files:

  • Location: “100_Project/01_dataviewjs/01_by_example/Q24_SumDuration/Q24_test_data”

folder: 03

  • filename : dic_19790302
---
Date: 1979-03-02
TimeInterval: 01:30:00
---
#Project/P03

orgWatchingDuration :: 90 minutes




  • filename : dic_19790305
---
Date: 1979-03-05
TimeInterval: 02:00:00
---
#Project/P03

orgWatchingDuration :: 120 minutes



  • filename : dic_19790316
---
Date: 1979-03-16
TimeInterval: 06:00:00
---
#Project/P03

orgWatchingDuration :: 360 minutes




folder: 08_null

  • filename : dic_19790801
---
Date: 1979-08-01
TimeInterval: 
---
#Project/P08

orgWatchingDuration :: 




folder: 09_undefined

  • filename : dic_19790901
---
Date: 1979-09-01

---
#Project/P09




DQL10_transform_timeInterval_into_Duration_and_sum_and_TABLE

Summary

Main DQL

Code Name Data type Group By Purposes Remark
DQL10
_transform_timeInterval
_into_Duration
_and_sum
_and_TABLE
TimeInterval:
a Time Interval like hh:mm:ss
yes 1.To filter by TimeInterval
2.To define a field variable hh
3.To define a field variable mm
4.To define a field variable ss
5.To define a field variable watchingDuration
6.To filter by watchingDuration
7.To sort by file.link in ascending order
8.To group by true
9.To display the result as a table
The DQL10 is based on the DQL10 in the following topic.
- Solutions: by Justdoitcc

Notes

Summary

Q1: How to transform a time interval like hh:mm:ss into a time duration?

Summary_Q1
A time interval
```md
TimeInterval :: 01:30:00
```
A time duration
```md
watchingDuration :: 1 hours, 30 minutes
```

A1: Original Example10 (DQL)

```dataview
FLATTEN number(split(TimeInterval, ":")[0]) AS hh
FLATTEN number(split(TimeInterval, ":")[1]) AS mm
FLATTEN number(split(TimeInterval, ":")[2]) AS ss
FLATTEN dur(hh+ " hours" + mm + " minutes" + ss + " seconds") AS watchingDuration
WHERE watchingDuration
```

A2: Another Example11 (DVJS)

title: Another Example11
collapse: close
icon: 
color:
```dataviewjs
// M11.define timeInterval:
// #####################################################################
let timeInterval = "01:30:00";


// M13.define watchingDuration:
// #####################################################################
// Create a Duration from an ISO 8601 time string
let watchingDuration = Duration.fromISOTime(timeInterval);


// M21.Output watchingDuration:
// #####################################################################
dv.span(watchingDuration); //=>1 hours, 30 minutes
dv.span("<br>");


// M31.Output watchingDuration:
// #####################################################################
// Convert this Duration into its representation in minutes
dv.span(watchingDuration.shiftTo("minutes").minutes); //=>90
   

```
Screenshots(A2):
```md
1 hours, 30 minutes
90   
```

Code DQL10_transform_timeInterval_into_Duration_and_sum_and_TABLE

Summary_code
title: DQL10_transform_timeInterval_into_Duration_and_sum_and_TABLE =>1.To filter by `TimeInterval` 2.To define a field variable `hh` 3.To define a field variable `mm` 4.To define a field variable `ss` 5.To define a field variable `watchingDuration` 6.To filter by `watchingDuration` 7.To sort by file.link in ascending order 8.To group by true 9.To display the result as a table
collapse: close
icon: 
color: 
```dataview
TABLE WITHOUT ID
      rows.file.link AS "File",
      rows.TimeInterval AS "TimeInterval",
      rows.watchingDuration AS "watchingDuration",
      sum(map(rows.watchingDuration, (e) => sum(default(e, 0)))) AS "sumDuration"  
      
FROM "100_Project/01_dataviewjs/01_by_example/Q24_SumDuration/Q24_test_data"
WHERE TimeInterval

FLATTEN number(split(TimeInterval, ":")[0]) AS hh
FLATTEN number(split(TimeInterval, ":")[1]) AS mm
FLATTEN number(split(TimeInterval, ":")[2]) AS ss
FLATTEN dur(hh+ " hours" + mm + " minutes" + ss + " seconds") AS watchingDuration
WHERE watchingDuration

SORT file.link ASC
GROUP BY true

```

Screenshots(DQL10):


DVJS10_transform_timeInterval_into_Duration_and_sum_and_TABLE

Summary

Main DVJS

Code Name Data type Group By Purposes Remark
DVJS10
_transform_timeInterval
_into_Duration
_and_sum
_and_TABLE
TimeInterval:
a Time Interval like hh:mm:ss
no 1.To transform a time interval like hh:mm:ss into a time duration
2.To sum a duration
3.To display the result as a table
1.The DVJS10 is based on the DVJS20 in the following topic.
- Solutions: by Justdoitcc

2.Each of the step M31, M33 and M51 in the DVJS10 is based on the DVJS10 in the following topic.
- Solutions: by Justdoitcc

code DVJS10_transform_timeInterval_into_Duration_and_sum_and_TABLE

Summary_code
title: DVJS10_transform_timeInterval_into_Duration_and_sum_and_TABLE =>1.To transform a time interval like `hh:mm:ss` into a time duration 2.To sum a duration 3.To display the result as a table
collapse: close
icon: 
color: 
```dataviewjs
// M11. define pages: gather relevant pages
// #####################################################################
let pages = dv
    .pages(
        '"100_Project/01_dataviewjs/01_by_example/Q24_SumDuration/Q24_test_data"'
    )
    .where((page) => page.TimeInterval)
    .sort((page) => page.file.name, "asc");


// M13.define watchingDuration:
// ##################################################################### 
// Create a Duration from an ISO 8601 time string
// let watchingDuration = Duration.fromISOTime(pages.TimeInterval);
let watchingDuration = pages.TimeInterval.map((e) => Duration.fromISOTime(e));


// M19.define sumDuration: To sum watchingDuration (milliseconds)
// the data type of sumDuration: duration
// #####################################################################
let sumDuration = dv.func.default(dv.func.sum(watchingDuration), 0);//=>9 hours, 30 minutes


// M21.define dur_sumDuration:
// the data type of dur_sumDuration: duration
// #####################################################################
// let dur_sumDuration = luxon.Duration.fromMillis(sumDuration);//=>9 hours, 30 minutes


// M29.Output sumDuration: Debug ONLY
// #####################################################################
// dv.span("total watching time:  ");
// // dv.span(dur_sumDuration);
// dv.span(sumDuration);


// ### M31.define : aoa_drinks
// #####################################################################
// dv.table(
//     ["Name", "Price", "Caffeine Content"],
//     [
//         ["Black Coffee", 120, 300],
//         ["Green Tea", 100, 200],
//         ["Apple Juice", 110, 0],
//         ["Iced Chocolate", 130, 0],
//         ["Hot Chocolate", 105, 6],
//     ]
// );

// ["File", "TimeInterval", "watchingDuration"]
let aoa_drinks = pages.map((page) => [
    page.file.link,
    page.TimeInterval,
    Duration.fromISOTime(page.TimeInterval),
]);



// ### M33.update aoa_drinks: push the_last_row_Total
// #####################################################################
// ["File", "Spend ($)", "Company", "Payment Method", "Total Spend ($)"]
// aoa_drinks.push([
//     "**Total**",
//     i_Total_Spend,
//     "",
//     "",
//     i_Total_Spend,
// ]);


// ["File", "TimeInterval", "watchingDuration"]
aoa_drinks["values"][aoa_drinks.length] = [
    "**Total**",
    "",
    sumDuration,
];


 
// ### M51. TABLE : aoa_drinks
// #####################################################################
// dv.table(
//     ["Name", "Price", "Caffeine Content"],
//     [
//         ["Black Coffee", 120, 300],
//         ["Green Tea", 100, 200],
//         ["Apple Juice", 110, 0],
//         ["Iced Chocolate", 130, 0],
//         ["Hot Chocolate", 105, 6],
//     ]
// );

// ["File", "TimeInterval", "watchingDuration"]
//dv.header(2, "M51.Spend Report in 2003");
dv.table(
    ["File", "TimeInterval", "watchingDuration"],
    aoa_drinks
);



Screenshots(DVJS10)


  • A solution:
    • You could replace this with dv.current() and it will work.
  • A recommendation:
    • You should avoid using JavaScript’s Reserved Words, built-in object, property and method names such as “length”.
  • Suppose that the original YAML name “length” is replaced with “TimeInterval” in any notes.

Topic

Summary
  • How to transform a time interval like hh:mm:ss into a time duration by using moment?

Test

Summary
  • dataview: v0.5.46

Input

Summary

the current file:

  • filename : test
---
TimeInterval: 01:30:00
---

### DVJS01






DVJS01_transform_timeInterval_into_Duration_by_using_moment

Summary_code

Notes

Summary_notes

Q1: How to fix the code as follows?

Summary_Q1
Original example
```JS
moment.duration(this.TimeInterval).asMinutes()
```

A1:

```JS
moment.duration(dv.current().TimeInterval).asMinutes()
```

Code DVJS01_transform_timeInterval_into_Duration_by_using_moment

title: DVJS01_transform_timeInterval_into_Duration_by_using_moment =>1.To require TimeInterval: 01:30:00 (defined in YAML)
collapse: close
icon: 
color: 
```dataviewjs
// M11.define timeInterval:
// #####################################################################
// let timeInterval = '01:30:00';
let timeInterval = dv.current().TimeInterval;


// M13.define watchingDuration:
// #####################################################################
// duration() : Create a Duration from an ISO 8601 time string
// asMinutes(): Convert this Duration into its representation in minutes
let watchingDuration = moment.duration(timeInterval).asMinutes();


// M31.Output watchingDuration:
// #####################################################################
dv.span(watchingDuration); //=>90


```

Screenshots(DVJS01):

```md
90   
```

Reference

Summary

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