Accessing task completion time values in dataviewjs

What I’m trying to do

I’m trying to get a list of tasks completed on a certain day with dataviewjs (I do know how to do it in dataview, but I need dataviewjs to manipulate it further).

Things I have tried

After an extensive search, here is a minimal example that should be working:


  • #task a task completed today :white_check_mark: 2025-01-13
const notes = dv.pages();
const tasks = notes.file.tasks.where(t => t.completion == dv.date("2025-01-13"));
dv.span(tasks.values.length)

The option for tracking tasks shorthand completion dates is on, and in the console I can see that the objects for the tasks does have the data as a datetime object.

I realize that the completion time object also contains hours and minutes and seconds, so the comparison in fact does not hold and should not work. I can’t see a better method built in, so I could just run separate comparisons for year and day and month. But I can’t find a way to even parse just the day/month/year out of the t.completion object.

dv.date("2025-01-13").year works to get the year out of the date object, but the same syntax (t.completion.year) does not work for the object in the dataview task list. From inspection in the console, the datetime objects seem to store values in a subproperty called “c”, but t.completion.c.year does not work either.

Does anybody know how to do this? Or is there a better way to just compare dates in the current dataview? I also tried:

t => dv.compare(t.completion, dv.date('2025-01-13') == -1)

where “-1” seems to be the value that dv.compare gives for matches on dates but not the rest of the timestamp. But that still doesn’t work.

I think the easiest way to do date comparisons is actually to transform the dates into ISO 8601 dates, and use string comparison. If you want to do actual date comparisons you need to take working with either the correct DateTime functions or moment.js functions get it correct.

So the simplest solution for a case like yours could be showcased with this note:

- [x] #task a task completed today ✅ 2025-01-14
- [ ] Not completed

## Completed tasks

```dataviewjs
const tasks = dv.current()
  .file.tasks
  .where(t => t.completion?.toFormat("yyyy-MM-dd") == "2025-01-14")
dv.taskList(tasks, false)
```

Here I check any tasks in the current file, and for each of them I format the date (if present) to an ISO 8601 date string which I then compare to my target date string.

Omg thanks so much @holroy, you saved my day!