# Datviewjs date manipulation

**URL:** <https://forum.obsidian.md/t/datviewjs-date-manipulation/44785>\
**Category:** Help\
**Tags:** dataview\
**Created:** [October 11, 2022, 5:34pm UTC](https://forum.obsidian.md/t/datviewjs-date-manipulation/44785 "2022-10-11T17:34:12Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![justdoitcc](https://forum.obsidian.md/user_avatar/forum.obsidian.md/justdoitcc/32/31704_2.png) [@justdoitcc](https://forum.obsidian.md/u/justdoitcc)\
**Post date:** [October 16, 2022, 4:05am UTC](https://forum.obsidian.md/t/datviewjs-date-manipulation/44785/3 "2022-10-16T04:05:05Z")

</div>

## Topic

> **Summary**
>
> - How to filter the data by 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/Q20\_SumDuration/Q20\_test\_data”
> - Suppose that today is “1987-03-08”
> - Suppose that the notes from the past seven days are to be taken into consideration.
> - Therefore, ONLY dic\_19870302 ,dic\_19870305 and dic\_19870308 are to be taken into consideration.
> 
> #### 03
> 
> - filename : `dic_19870302`
> 
> ```YAML
> ---
> Date: 1987-03-02
> ---
> #Project/P03
> 
> WorkoutDuration:: 90 minutes
> 
> ```
> 
> * * *
> 
> - filename : `dic_19870305`
> 
> ```YAML
> ---
> Date: 1987-03-05
> ---
> #Project/P03
> 
> WorkoutDuration:: 120 minutes
> 
> ```
> 
> * * *
> 
> #### 04\_list
> 
> - filename : `dic_19870308`
> 
> ```YAML
> ---
> Date: 1987-03-08
> ---
> #Project/P03
> 
> WorkoutDuration:: 3 minutes, 7 minutes
> 
> ```
> 
> * * *
> 
> #### 07\_excluded
> 
> - filename : `dic_19870316`
> 
> ```YAML
> ---
> Date: 1987-03-16
> ---
> #Project/P03
> 
> WorkoutDuration:: 360 minutes
> 
> ```
> 
> * * *
> 
> #### 08\_null
> 
> - filename : `dic_19870801`
> 
> ```YAML
> ---
> Date: 1987-08-01
> ---
> #Project/P08
> 
> WorkoutDuration:: 
> 
> ```
> 
> * * *
> 
> #### 09\_undefined
> 
> - filename : `dic_19870901`
> 
> ```YAML
> ---
> Date: 1987-09-01
> ---
> #Project/P09
> 
> ```

* * *

## DQL10\_filter\_by\_duration\_and\_sum\_duration\_and\_LIST

> **Summary**
>
> ### Main DQL
> 
> | Code Name | Data type | Group By | Purposes | Remark |
> | --- | --- | --- | --- | --- |
> | DQL10  
> \_filter\_by\_duration  
> \_and  
> \_sum\_duration  
> \_and\_LIST | `WorkoutDuration`:  
> a duration   
> or a list of durations | yes | 0.“today” = “1987-03-08”   
> 1.To filter by `WorkoutDuration`   
> 2.To filter by `file.day`   
> 3.To define a field variable `F_dur_diff` and `let F_dur_diff = date("1987-03-08") - file.day;`  
> 4.To filter by `F_dur_diff`   
> 5.To sort by file.link in descending order   
> 6.To group by true   
> 7.To display the result as a non-list | 1.Suppose that today is “1987-03-08”  
>   
> 2.Suppose that the notes from the past seven days are to be taken into consideration.   
>   
> 3.The sum expression in the DQL10 is based on the Conclusion in the following topic.  
> - Solutions: [by Justdoitcc](https://forum.obsidian.md/t/dataview-plugin-snippet-showcase/13673/658) |
> 
> ### Notes
> 
> > **Summary**
> >
> > #### to modify: “today” = “1987-03-08”
> > 
> > ##### Original Example10: use date(“1987-03-08” )
> > 
> > ````auto
> > ```SQL
> > FLATTEN date("1987-03-08") - file.day AS F_dur_diff
> > ```
> > 
> > ````
> > 
> > ##### Another Example11: use date(“today”)
> > 
> > ````auto
> > ```SQL
> > FLATTEN date("today") - file.day AS F_dur_diff
> > ```
> > 
> > ````
> > 
> > #### The same codes:
> > 
> > - To filter the data by duration
> > 
> > ##### Original Example10
> > 
> > ````auto
> > ```SQL
> > FLATTEN date("today") - file.day AS F_dur_diff
> > WHERE F_dur_diff <= dur("7 days") AND F_dur_diff >= dur("0 days")
> > ```
> > 
> > ````
> > 
> > ##### Another Example11
> > 
> > ````auto
> > ```SQL
> > FLATTEN (date("today") - file.day).days AS F_dur_days
> > WHERE F_dur_days <= 7 AND F_dur_days >= 0
> > ```
> > 
> > ````
> > 
> > ##### Another Example10B
> > 
> > ````auto
> > ```SQL
> > 
> > WHERE date("today") - file.day <= dur("7 days") AND date("today") - file.day >= dur("0 days")
> > ```
> > 
> > ````
> > 
> > ##### Another Example11B
> > 
> > ````auto
> > ```SQL
> > 
> > WHERE (date("today") - file.day).days <= 7 AND (date("today") - file.day).days >= 0
> > ```
> > 
> > ````
> > 
> > ##### Another Example10C
> > 
> > ````auto
> > ```SQL
> > 
> > WHERE date("today") - file.day <= dur("7 days") 
> > WHERE date("today") - file.day >= dur("0 days")
> > ```
> > 
> > ````
> > 
> > ##### Another Example11C
> > 
> > ````auto
> > ```SQL
> > 
> > WHERE (date("today") - file.day).days <= 7 
> > WHERE (date("today") - file.day).days >= 0
> > ```
> > 
> > ````
> 
> * * *
> 
> ### Code DQL10\_filter\_by\_duration\_and\_sum\_duration\_and\_LIST
> 
> > **Summary\_code**
> >
> > ````ad-info
> > title: DQL10_filter_by_duration_and_sum_duration_and_LIST =>0."today" = "1987-03-08" 1.To filter by `WorkoutDuration` 2.To filter by `file.day` 3.To define a field variable `F_dur_diff` and `let F_dur_diff = date("1987-03-08") - file.day;` 4.To filter by `F_dur_diff` 5.To sort by file.link in descending order 6.To group by true 7.To display the result as a non-list
> > collapse: close
> > icon: 
> > color: 
> > ```dataview
> > LIST WITHOUT ID
> > "total workout time: " + sum(map(rows.WorkoutDuration, (e) => sum(default(e, 0))))
> > 
> > FROM "100_Project/01_dataviewjs/01_by_example/Q20_SumDuration/Q20_test_data"
> > WHERE file.day != null
> > WHERE WorkoutDuration != null
> > 
> > FLATTEN date("1987-03-08") - file.day AS F_dur_diff
> > WHERE F_dur_diff <= dur("7 days") AND F_dur_diff >= dur("0 days")
> > 
> > SORT file.link DESC
> > GROUP BY true
> > 
> > ```
> > 
> > ````
> 
> * * *
> 
> #### Screenshots(DQL10): “today” = “1987-03-08”
> 
> ![20221017_Q20_DQL10_output_a01](https://forum.obsidian.md/uploads/default/original/3X/6/c/6c28c6246f75b3eb824550c5fa3c67b084beca59.png)

* * *

## DVJS10\_debug\_the\_first\_original\_code\_of\_kjz

> **Summary**
>
> ### Main DVJS
> 
> | Code Name | Data type | Group By | Purposes | Remark |
> | --- | --- | --- | --- | --- |
> | DVJS10  
> \_debug  
> \_the\_first  
> \_original\_code  
> \_of\_kjz | `WorkoutDuration`:  
> a duration   
> or a list of durations | no | To debug the first original code of kjz | Suppose that today is “1987-03-08” |
> 
> ### Notes
> 
> > **Summary**
> >
> > #### to modify(M11): “today” = “1987-03-08”
> > 
> > ##### Original Example10: use dv.date(“1987-03-08” )
> > 
> > ````auto
> > ```JS
> > // M11. define pagesPre: gather relevant pages
> > // #####################################################################
> > //const pagesPre = dv.pages('"Workouts/2022"')
> > let pagesPre = dv
> > .pages(
> > '"100_Project/01_dataviewjs/01_by_example/Q20_SumDuration/Q20_test_data"'
> > )
> > //Added by Justdoitcc 2022-10-16 10:30
> > .where((page) => page.file.day) 
> > .where((page) => page.WorkoutDuration)
> > .where((page) => dv.date("1987-03-08") - page.file.day <= dv.duration("7 days"))
> > .where((page) => dv.date("1987-03-08") - page.file.day >= dv.duration("0 days"))    
> >     
> > .sort((k) => k.file.name, "desc");
> > ```
> > 
> > ````
> > 
> > ##### Another Example11: use dv.date(“today”)
> > 
> > ````auto
> > ```JS
> > // M11. define pagesPre: gather relevant pages
> > // #####################################################################
> > //const pagesPre = dv.pages('"Workouts/2022"')
> > let pagesPre = dv
> > .pages(
> > '"100_Project/01_dataviewjs/01_by_example/Q20_SumDuration/Q20_test_data"'
> > )
> > //Added by Justdoitcc 2022-10-16 10:30
> > .where((page) => page.file.day) 
> > .where((page) => page.WorkoutDuration)
> > .where((page) => dv.date("today") - page.file.day <= dv.duration("7 days"))
> > .where((page) => dv.date("today") - page.file.day >= dv.duration("0 days"))    
> >     
> > .sort((k) => k.file.name, "desc");
> > ```
> > 
> > ````
> 
> * * *
> 
> ### code DVJS10\_debug\_the\_first\_original\_code\_of\_kjz
> 
> > **Summary\_code**
> >
> > ````ad-info
> > title: DVJS10_debug_the_first_original_code_of_kjz =>0."today" = "1987-03-08" 
> > collapse: close
> > icon: 
> > color: 
> > ```dataviewjs
> > // M11. define pagesPre: gather relevant pages
> > // #####################################################################
> > //const pagesPre = dv.pages('"Workouts/2022"')
> > let pagesPre = dv
> > .pages(
> > '"100_Project/01_dataviewjs/01_by_example/Q20_SumDuration/Q20_test_data"'
> > )
> > //Added by Justdoitcc 2022-10-16 10:30
> > .where((page) => page.file.day) 
> > .where((page) => page.WorkoutDuration)
> > .where((page) => dv.date("1987-03-08") - page.file.day <= dv.duration("7 days"))
> > .where((page) => dv.date("1987-03-08") - page.file.day >= dv.duration("0 days"))
> >     
> > .sort((k) => k.file.name, "desc");
> > 
> > // M11.DEB10 debug pagesPre: 
> > // #####################################################################
> > dv.span("##### M11.DEB10 debug pagesPre:");
> > dv.span(pagesPre.file.name);
> > dv.span(pagesPre.file.day);
> > dv.span(pagesPre.WorkoutDuration);
> > 
> > // M21.define pages:
> > // #####################################################################
> > //Remarked by Justdoitcc 2022-10-16 10:35
> > //Added by Justdoitcc 2022-10-15 13:30
> > //let pages = [];
> > 
> > // M23.update pages: 
> > // #####################################################################
> > //Remarked by Justdoitcc 2022-10-16 10:35
> > // let n = 0;
> > // for (let i = 0; i < pagesPre.length; i++) {
> > // // M21.IF10 filter pagesPre:
> > // // #####################################################################
> > // //Remarked by Justdoitcc 2022-10-15 13:22
> > // //if(dv.date(today) - pagesPre[i].file.day <= dur(7 days)) {
> > // if (
> > // dv.date("today") - pagesPre[i].file.day <= dv.duration("7 days") &&
> > // // Added by Justdoitcc 2022-10-15 13:10
> > // dv.date("today") - pagesPre[i].file.day >= dv.duration("0 days")
> > // ) {
> > // //Remarked by Justdoitcc 2022-10-15 13:30
> > // //let pages[n] = pagesPre[i];
> > // pages[n] = pagesPre[i];
> > // n += 1;
> > // }
> > // }
> > 
> > // M31.define sumDuration: To sum WorkoutDuration (milliseconds)
> > // #####################################################################
> > //Added by Justdoitcc 2022-10-16 10:40
> > let pages = pagesPre;
> > 
> > let sumDuration = 0;
> > for (let i = 0; i < pages.length; i++) {
> > //Remarked by Justdoitcc 2022-10-16 10:40
> > // if (pages[i].WorkoutDuration) {
> > // sumDuration += pages[i].WorkoutDuration;
> > // }
> > sumDuration += pages[i].WorkoutDuration;
> > }
> > 
> > //dv.span(sumDuration);
> > //dv.span(dv.func.typeof(sumDuration));
> > 
> > // M33.define sumHours and minutes:
> > // #####################################################################
> > //let sumHours = sumDuration / 60;
> > 
> > //Remarked by Justdoitcc 2022-10-15 13:40
> > //let sumHours = Math.floor(sumDuration/60);
> > //let minutes = sumDuration % 60;
> > 
> > let sumHours = Math.floor(sumDuration / 60 / 60 / 1000);
> > let minutes = (sumDuration / 60 / 1000) % 60;
> > 
> > // M41.Output sumDuration:
> > // #####################################################################
> > dv.span("##### M41.Output sumDuration:");
> > dv.paragraph(
> > "total workout time: " + sumHours + " hours " + minutes + " minutes "
> > );
> > 
> > ````
> 
> * * *
> 
> #### Screenshots(DVJS10): “today” = “1987-03-08”
> 
> ![20221016_Q20_DVJS10_output_a01](https://forum.obsidian.md/uploads/default/original/3X/4/1/419e2cb9eb811537c7e08985aca68765bb1262a4.png)

* * *

## DVJS20\_filter\_by\_duration\_and\_sum\_duration\_and\_SPAN

> **Summary**
>
> ### Main DVJS
> 
> | Code Name | Data type | Group By | Purposes | Remark |
> | --- | --- | --- | --- | --- |
> | DVJS20  
> \_filter\_by\_duration  
> \_and\_sum\_duration  
> \_and\_SPAN | `WorkoutDuration`:  
> a duration   
> or a list of durations | no | To filter the data by duration and sum a duration in multiple pages | 1.Suppose that today is “1987-03-08”   
> 2.Suppose that the notes from the past seven days are to be taken into consideration.  
> 3.The sum expression in the DVJS20 is based on the Conclusion in the following topic.  
> - Solutions: [by Justdoitcc](https://forum.obsidian.md/t/dataview-plugin-snippet-showcase/13673/658) |
> 
> ### Notes
> 
> > **Summary**
> >
> > #### to modify(M11): “today” = “1987-03-08”
> > 
> > ##### Original Example10: use dv.date(“1987-03-08” )
> > 
> > ````auto
> > ```JS
> > // M11. define pages: gather relevant pages
> > // #####################################################################
> > let pages = dv
> > .pages(
> > '"100_Project/01_dataviewjs/01_by_example/Q20_SumDuration/Q20_test_data"'
> > )
> > .where((page) => page.file.day) 
> > .where((page) => page.WorkoutDuration)
> > .where((page) => dv.date("1987-03-08") - page.file.day <= dv.duration("7 days"))
> > .where((page) => dv.date("1987-03-08") - page.file.day >= dv.duration("0 days")) 
> > .sort((page) => page.file.name, "desc");
> > ```
> > 
> > ````
> > 
> > ##### Another Example11: use dv.date(“today”)
> > 
> > ````auto
> > ```JS
> > // M11. define pages: gather relevant pages
> > // #####################################################################
> > let pages = dv
> > .pages(
> > '"100_Project/01_dataviewjs/01_by_example/Q20_SumDuration/Q20_test_data"'
> > )
> > .where((page) => page.file.day) 
> > .where((page) => page.WorkoutDuration)
> > .where((page) => dv.date("today") - page.file.day <= dv.duration("7 days"))
> > .where((page) => dv.date("today") - page.file.day >= dv.duration("0 days"))    
> > .sort((page) => page.file.name, "desc");
> > ```
> > 
> > ````
> 
> * * *
> 
> ### code DVJS20\_filter\_by\_duration\_and\_sum\_duration\_and\_SPAN
> 
> > **Summary\_code**
> >
> > ````ad-info
> > title: DVJS20_filter_by_duration_and_sum_duration_and_SPAN =>0."today" = "1987-03-08" 1.To filter the data by duration and sum a duration in multiple pages
> > collapse: close
> > icon: 
> > color: 
> > ```dataviewjs
> > // M11. define pages: gather relevant pages
> > // #####################################################################
> > let pages = dv
> > .pages(
> > '"100_Project/01_dataviewjs/01_by_example/Q20_SumDuration/Q20_test_data"'
> > )
> > .where((page) => page.file.day) 
> > .where((page) => page.WorkoutDuration)
> > .where((page) => dv.date("1987-03-08") - page.file.day <= dv.duration("7 days"))
> > .where((page) => dv.date("1987-03-08") - page.file.day >= dv.duration("0 days")) 
> > .sort((page) => page.file.name, "desc");
> > 
> > // M21.define sumDuration: To sum WorkoutDuration (milliseconds)
> > // the data type of sumDuration: number
> > // #####################################################################
> > let sumDuration = dv.func.default(dv.func.sum(pages.WorkoutDuration), 0);
> > 
> > // M31.define dur_sumDuration:
> > // the data type of dur_sumDuration: duration
> > // #####################################################################
> > let dur_sumDuration = luxon.Duration.fromMillis(sumDuration);
> > 
> > // M41.Output dur_sumDuration:
> > // #####################################################################
> > dv.span("total workout time: ");
> > dv.span(dur_sumDuration);
> > 
> > ````
> 
> * * *
> 
> #### Screenshots(DVJS20): “today” = “1987-03-08”
> 
> ![20221016_Q20_DVJS20_output_a01](https://forum.obsidian.md/uploads/default/original/3X/f/f/ff037c871ee48ea8969f2983c911ff7b10025af6.png)

* * *

---

_[View the full topic](https://forum.obsidian.md/t/datviewjs-date-manipulation/44785)._
