How to change task result date background color (dataview plugin)

It’s the first time I post here and English is not my first language so I hope it’s understandable. (^_^)v

The dataview below only displays tasks that are due in the month.

WHERE contains(text, "due")
WHERE !completed AND due.month = date(today).month AND due.year = date(today).year


I just wanted that, if the day of the task is the same as today, the background changes to red.

the only things I was able to change manually were the background and text colors in dataview > styles

.dataview.inline-field-standalone-value {
    padding-left: 8px;
    padding-right: 8px;
    font-family: var(--font-monospace);
    background-color: var(--background-secondary-alt);
    color: var(--text-nav-selected);

From DQL side you can’t do anything.
I think via CSS neither (because date(today) is a dynamic value).
Change the output of the tasks text is possible only in dvjs (dataviewjs), but it is a complex way and implies the ability to define the style inside the dataview rendered container (and I don’t know if it’s possible to target the style of specific value).

1 Like

It is possible to change the formatting based upon the text, but it would preferably be some static/unique text to have as a target. And it’s not a route I would recommend, if you’re not familiar with CSS (and possibly Dataview js).

A lot easier task would be to separate your query into two queries, one for tasks for today, and one for tasks for this month (possibly excluding todays task). This would make todays tasks pop out, since they’re in their own table. :smiley:

1 Like

Your suggestion helped me to think about how to reduce the result to a week.
It wasn’t exactly what I wanted but I understood that I need to study more! (^_^)v

WHERE contains(text, "due")
WHERE !completed AND due - date(today) <= dur(7 days)
SORT (due) ASC


I’d like to write here one example in JS exploring your goal. But before I need to clarify some points:

  • I’m not a coder, programmer or similar. I don’t have any code skills, only curiosity in Obsidian.
  • If I acquired some skills in regular dql, js remains an unknown world. So, the js code bellow is ugly, not clean, and probably with many logical faults… And dates in javascript are a darkness field…
  • If any js expert want, please optimize/debug the query code (in particular the repetitions… I don’t know write functions)

Sample notes

# 2022-11-22
- [ ] Quisque dictum aliquet tortor (due:: 2022-11-30)
- [ ] Vestibulum sollicitudin neque
- [ ] Nam ac dignissim libero (due:: 2022-11-22)
- [ ] Nulla et ultrices erat (due:: 2022-12-02)
# 2022-11-23
- [ ] Lorem ipsum dolor sit amet (due:: 2022-11-25)
- [ ] Ut luctus, ex non semper ultrices
- [ ] Cras volutpat convallis orci nec tristique (due:: 2022-11-26)
- [ ] Nunc lobortis vehicula augue eget eleifend (due:: 2022-12-05)
# 2022-11-24
- [ ] Cras volutpat convallis (due:: 2022-11-29)
- [ ] Morbi finibus faucibus augue
- [ ] At suscipit dui dapibus vitae (due:: 2022-11-26)
- [ ] Gravida luctus nisl neque at nisl (due:: 2022-12-05)
- [ ] Falicius task (due:: 2022-10-30)

The query

This is a dvjs query to list all open tasks with due in current month. The main goal is: highlight the tasks with due date equal to the current date (today) and tasks with overdue date.

// define pages
const pages = dv.pages('"your/folder/path"')

// filter open tasks with due date
const tasks = pages.file.tasks.where(t => !t.completed && t.due)

// filter tasks with due date in the current month
const tasksMonth = tasks.where(t => t.due.toFormat("yyyy-MM") =='today').toFormat("yyyy-MM"))

// regex to remove the field due in text ( I don't know regex... It's a stolen expression)
const regex = /\(due[^\)]+\)/g

// colors for due dates
const green = "rgba(0, 132, 98, 0.7)"
const red = "rgba(211, 60, 43, 0.7)"
const normal = "var(--text-muted)"

// assign colors according to due date - green for today dates; red for overdue dates
for (let task of tasksMonth) {
	if (task.due.ts =='today').ts) {
		task.visual = task.text.replace(regex, "") +
		`<span style= "border-radius:5px; padding:2px 5px; font-size:8pt; background-color:${green};">${task.due.toFormat("yyyy-MM-dd")}</span>`
	if (task.due <'today')) {
		task.visual = task.text.replace(regex, "") +
		`<span style= "border-radius:5px; padding:2px 5px; font-size:8pt; background-color:${red};">${task.due.toFormat("yyyy-MM-dd")}</span>`
	if (task.due >'today')) {
		task.visual = task.text.replace(regex, "") +
		`<span style= "border-radius:5px; padding:2px; font-size:8pt; color:${normal};">${task.due.toFormat("yyyy-MM-dd")}</span>`

// render tasks sorted by due date
dv.taskList(tasksMonth.sort(t => t.due, 'asc'), false)


1 Like

I think I’m in the same place, I’m not a programmer but I’m curious and obsidian allows that!
With this code I can try to learn more, thank you very much for your time and help! (^_^)v

1 Like

doing some tests I noticed that showing the tasks of the month did not show the following month. And if it’s the last day of the month and I have an appointment on the first day of the following month maybe time is short to find out.

I found a way:

const tasksMonth = tasks.where ( t => t.due <="today").plus({days: 7}))

and I can change days to whatever I want! (^_^)v


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