blacksmithgu / obsidian-dataview

A data index and query language over Markdown files, for https://obsidian.md/.
https://blacksmithgu.github.io/obsidian-dataview/
MIT License
7.06k stars 414 forks source link

the Date calculation method might have some errors #1966

Open Eplicony opened 1 year ago

Eplicony commented 1 year ago

What happened?

image

Here we can see the date(today) is 6 22, the date of file "2023-5-24" is 5 24, but the day time interval between them is 31, making the table not an strictly increasing order.

I think there might be some errors in date calculation method or maybe some details I ignored.

DQL

table file.cday As Day, date(today), (date(today)-file.cday).day AS "Diff day", date(today)-file.cday AS values
from #📝 
where true
OR (date(today)-file.cday).day = 1
OR (date(today)-file.cday).day = 3
OR (date(today)-file.cday).day = 5
OR (date(today)-file.cday).day = 9
OR (date(today)-file.cday).day = 16
OR (date(today)-file.cday).day = 29
OR (date(today)-file.cday).day = 60
OR (date(today)-file.cday).day = 100
OR (date(today)-file.cday).day = 200
OR (date(today)-file.cday).day = 365
sort file.cday DESC

JS

No response

Dataview Version

0.5.56

Obsidian Version

v1.3.5

OS

MacOS

GovSat1 commented 1 year ago

This is a long-standing issue due to luxon date formats. As a workaround, you can convert to unix-time and divide. Try: (( number(dateformat(date(today), "X")) - number(dateformat(date(file.cday), "X")) ) / 86400 ) AS "Diff Day"

bharker75 commented 10 months ago

As a workaround, you can convert to unix-time and divide. Try: (( number(dateformat(date(today), "X")) - number(dateformat(date(file.cday), "X")) ) / 86400 ) AS "Diff Day"

I encountered this bug today and kept looking for ways to convert dates to something that would math properly and found your fix. Thanks for the workaround @GovSat1 .

bharker75 commented 9 months ago

A note that if the dates cross daylight savings you'll end up with fractions so adding a round(,0) is the easy fix:

round((( number(dateformat(date(today), "X")) - number(dateformat(date(file.cday), "X")) ) / 86400 ),0) AS "Diff Day"