Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odd date comparison calculation #2466

Open
elingamfelter opened this issue Oct 31, 2024 · 1 comment
Open

Odd date comparison calculation #2466

elingamfelter opened this issue Oct 31, 2024 · 1 comment
Labels
bug Something isn't working.

Comments

@elingamfelter
Copy link

What happened?

I am comparing the number of days between two dates. In the provided DQL snippet, the .days output jumps from 27 to 30 even though I only changed the date by 1 day.

It seems this is because the calculation goes from weeks to month (30 days).

Is there a better way to write this calculation?

DQL

`= date(2024-10-29) - date(2024-10-02)`        // 3 weeks, 6 days
`= (date(2024-10-29) - date(2024-10-02)).days` // 27

`= date(2024-10-30) - date(2024-10-02)`        // 1 month
`= (date(2024-10-30) - date(2024-10-02)).days` // 30

JS

No response

Dataview Version

0.5.67

Obsidian Version

1.7.4

OS

MacOS

@elingamfelter elingamfelter added the bug Something isn't working. label Oct 31, 2024
@holroy
Copy link
Contributor

holroy commented Dec 28, 2024

I'm not sure why Dataview does this, or if this is an effect coming from the Luxon date library. I reckon it's an attempt to produce humanised duration formats. When I've come across this issue myself, I've either switched to using dataviewjs and using the moment.js library directly, or done the calculations using epoch times (aka seconds since 1970-01-01).

The latter could then look like:

29: `= trunc((number(dateformat(date(2024-10-29), "X")) - number(dateformat(date(2024-10-02), "X"))) / (24 * 60 * 60 ))`   
30: `= trunc((number(dateformat(date(2024-10-30), "X")) - number(dateformat(date(2024-10-02), "X"))) / (24 * 60 * 60 ))`   
31: `= trunc((number(dateformat(date(2024-10-31), "X")) - number(dateformat(date(2024-10-02), "X"))) / (24 * 60 * 60 ))`   

Come to think of it, maybe it could be a possible workaround to provide a function like epochday( date ) which produce the number of days since epoch, for usage in calculations like these? Then it could look like: `= epochday(date(2024-10-30)) - epochday(date(2024-10-02)) `, and you'd get the pure day number calculation...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working.
Projects
None yet
Development

No branches or pull requests

2 participants