Dates queries

Hi all,

Have one question. After migration to V2 some of my queries stopped working due to the date comparison.

${template.each( query[[ from index.tag 'task' where deadline < os.date("%d-%m-%Y") and not done ]], templates.taskItem )}

For query above I have error:

Lua error: attempt to compare object with string

i just had the same problem for an hour, at some point it started working, now idea why, try copy/pasting what I did and go from there if it works :person_shrugging:

- event future #evenement [date:2026-06-27]
- even past #evenement [date:2025-01-07]

${query[[
  from index.tag "evenement"
  where date >= os.date("%Y-%m-%d")
  order by _.date
  select { 
    event = '[[' .. _.ref .. '|' .. _.name .. ']]',
    date = _.date
  }
]]}

If you’re using the legacy :alarm_clock: YYYY-mm-dd format I’d recommend moving away from that, it will go away in the next release, use the attribute syntax instead, eg [deadline: "YYYY-mm-dd"]

What may also go wrong here is that most tasks don’t have a deadline which means it is nil and comparing that with a string is not legal (probably that’s where the error comes from). You need to check for a value first, eg where _.deadline and _.deadline < …

1 Like

Curious, is [deadline: “YYYY-mm-dd”] different from [deadline: YYYY-mm-dd] (no quotes)? Is one preferable? Both seem to work fine for me with queries.

If you use it without the quotes, YAML interprets (or tries to) interpret it as a YAML date, which SB later translates back to a string again, some surprising things may happen here. Not sure how it deals with time zones for instance. I’m not a heavy “date user” myself so haven’t looked too deeply into this.

1 Like

Got it, thanks. Does it make sense for SB to retain the YAML type in these instances?

I don’t think so, because those would end up being Date JavaScript objects. If we’d go that way all dates everywhere should be encoded that way (including Lua APIs) and I’m not sure the data layer evens supports this. Anyway, strings everywhere is simpler.

1 Like

Hi,

I think I was able to solve this issue. Probably there is a problem with comparison in case where there is a null deadline. So I added one additional statement:

${template.each( query[[ from index.tag 'task' where (_.deadline == date.today()) or (_.deadline != empty and _.deadline > date.today()) and not done ]], templates.taskItem )}