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
);