Skip to content

Instantly share code, notes, and snippets.

@TfTHacker
Last active January 15, 2024 15:50
Show Gist options
  • Save TfTHacker/4e019abd25c58de57376add6e3aa4173 to your computer and use it in GitHub Desktop.
Save TfTHacker/4e019abd25c58de57376add6e3aa4173 to your computer and use it in GitHub Desktop.
```dataviewjs
// find dates based on format [[YYYY-MM-DD]]
const findDated = (task)=>{
if( !task.completed ) {
task.link = " " + "[[" + task.path + "|*]]";
task.date="";
const found = task.text.match(/\[\[([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))\]\]/);
if(found) task.date = moment(found[1]);
return true;
}
}
const myTasks = dv.pages("").file.tasks.where(t => findDated(t));
dv.header(1,"Overdue");
dv.table(["task","link"], myTasks.filter(t=> moment(t.date).isBefore(moment(),"day")).sort(t=>t.date).map(t=>[t.text, t.link]));
dv.header(1,"Today");
dv.table(["task","link"], myTasks.filter(t=> moment(t.date).isSame(moment(),"day")).sort(t=>t.date).map(t=>[t.text, t.link]));
dv.header(1,"Upcoming");
dv.table(["task","link"], myTasks.filter(t=> moment(t.date).isAfter(moment(),"day")).sort(t=>t.date).map(t=>[t.text, t.link]));
dv.header(1,"Undated");
dv.table(["task","link"], myTasks.filter(t=> !t.date).sort(t=>t.text).map(t=>[t.text, t.link]));
```
@datavistics
Copy link

Is there a way to mark a task completed? I know with raw dataview you can, so I figure there is a way to do this in dataviewjs too.

@thomas-hu-bc
Copy link

Beautiful code, I have same question: is there a way to mark a task completed like the raw dataview do? Million thanks

@TfTHacker
Copy link
Author

Sorry for delayed response. At this point in time I don’t think its possible through the script. but I am not currently using it, so have not been testing new possibilities :-)

@Tristan29photography
Copy link

Tristan29photography commented Aug 2, 2022

@thomas-hu-bc @datavistics

You can get the clickable task list with this code :


// find dates based on format [[YYYY-MM-DD]]

const findDated = (task)=>{

if( !task.completed ) {

task.link = " " + "[[" + task.path + "|*]]";

task.date="";

const found = task.text.match(/\[\[([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))\]\]/);

if(found) task.date = moment(found[1]);

return true;

}

}
const myTasks = dv.pages('"3 Projects"').file.tasks.where(t => findDated(t));

dv.header(2,"Overdue");

dv.taskList(myTasks.filter(t=> moment(t.date).isBefore(moment(),"day")).sort(t=>t.date));

dv.header(2,"Today");

dv.taskList(myTasks.filter(t=> moment(t.date).isSame(moment(),"day")).sort(t=>t.date));

dv.header(2,"Upcoming");

dv.taskList(myTasks.filter(t=> moment(t.date).isAfter(moment(),"day")).sort(t=>t.date));

I hope it helps

and @TfTHacker Thanks a lot for your code !

@rawru
Copy link

rawru commented Aug 19, 2022

Your code works absolutely amazing but unfortunately, I use a different date format (DD-MM-YYYY). I'm not exactly familiar javascript, so... how can I change the code to match my date format? Thanks in advance!

@creadian
Copy link

This is great, thanks so much @TfTHacker and @Tristan29photography !
Do you know if it's possible that clicking the link automatically brings you to the location within the note where the task is, instead of opening the note? I work with very long notes, and sometimes need the task's location for context.

@rawru I don't know how to code and this is probably a very stupid suggestion, but did you try
// find dates based on format [[DD-MM-YYYY]]?

@Razortide
Copy link

Hey @creadian, what you are referring to is a comment. So this is just a description of what the code will do.

What you want to change is this:

const found = task.text.match(/[[([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))]]/);

That cryptic part is a regex, a pattern that it searches. You could try this (I've not tested that yet).

const found = task.text.match(/[[(((0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-[12]\d{3}))]]/);

@Razortide
Copy link

@thomas-hu-bc @datavistics

You can get the clickable task list with this code :


// find dates based on format [[YYYY-MM-DD]]

const findDated = (task)=>{

if( !task.completed ) {

task.link = " " + "[[" + task.path + "|*]]";

task.date="";

const found = task.text.match(/\[\[([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))\]\]/);

if(found) task.date = moment(found[1]);

return true;

}

}
const myTasks = dv.pages('"3 Projects"').file.tasks.where(t => findDated(t));

dv.header(2,"Overdue");

dv.taskList(myTasks.filter(t=> moment(t.date).isBefore(moment(),"day")).sort(t=>t.date));

dv.header(2,"Today");

dv.taskList(myTasks.filter(t=> moment(t.date).isSame(moment(),"day")).sort(t=>t.date));

dv.header(2,"Upcoming");

dv.taskList(myTasks.filter(t=> moment(t.date).isAfter(moment(),"day")).sort(t=>t.date));

I hope it helps

and @TfTHacker Thanks a lot for your code !

Question: How would you sort for Undated Tasks?

@friebetill
Copy link

friebetill commented Nov 8, 2022

Here is my customized version of your scripts to show me an overview of the tasks due in my daily notes.

What is the difference?

  • Sections with empty task lists are not displayed.
  • Tasks are recognized with the #task hashtag.
  • Tasks with #habit are ignored.
  • The date format is YYYY-MM-DD.
const hasDate = (task) => {
  const found = task.text.match("📅 ([0-9]{4}-[0-9]{2}-[0-9]{2})");
  task.date = found ? moment(found[1]) : moment("");
  return found != null;
}

const today = moment("{{date:YYYY-MM-DD}}");

const allTasks = dv.pages("").file.tasks.filter((t) => t.text.match("#task") && !t.text.match("#habit")); 
const uncompletedTasks = allTasks.filter((t) => !t.completed);
const dateTasks = uncompletedTasks.filter(hasDate);
const noDateTasks = uncompletedTasks.filter(t => !hasDate(t));

const overdueTasks = dateTasks.filter(t=>moment(t.date).isBefore(today,"day")).sort(t=>t.date);
if(overdueTasks.length > 0) {
  dv.header(2, "Overdue");
  dv.taskList(overdueTasks);
}

const dueTasks = dateTasks.filter(t=>moment(t.date).isSame(today,"day")).sort(t=>t.date);
if (dueTasks.length > 0) {
  dv.header(2, "Today");
  dv.taskList(dueTasks);
}

if (noDateTasks.length > 0) {
  dv.header(2, "No deadline");
  dv.taskList(noDateTasks);
}

@timoz
Copy link

timoz commented Jan 5, 2023

thanks for the code- very useful. I have been using this on my daily note, but I would like to exclude tasks from "today's" note. My dailynotes are all named "YYYY-MM-DD" so I figured I just need to add in something like. dv.pages => ! "dv.current().date" ... but not too clued up on this yet

@AC12344
Copy link

AC12344 commented Feb 3, 2023

Hey @creadian, what you are referring to is a comment. So this is just a description of what the code will do.

What you want to change is this:

const found = task.text.match(/[[([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))]]/);

That cryptic part is a regex, a pattern that it searches. You could try this (I've not tested that yet).

const found = task.text.match(/[[(((0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-[12]\d{3}))]]/);

I couldnt get that working, but const found = task.text.match(/([0-9]{2}-[0-9]{2}-[0-9]{4})/); like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment