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

Wrong date calculation? #181

Closed
Moonbase59 opened this issue May 4, 2021 · 2 comments
Closed

Wrong date calculation? #181

Moonbase59 opened this issue May 4, 2021 · 2 comments

Comments

@Moonbase59
Copy link

@Moonbase59 Moonbase59 commented May 4, 2021

I’m showing "current age in years" in my people notes, but the calculation result is wrong: It shows a wrong age.

Today is 2021-05-04, and the person’s birthday is 1997-05-17, so she is clearly 23 years of age and about to turn 24 on May 17.

I have in the front matter:

---
birthday: 1997-05-17
---

and I use

`= (date(today) - this.birthday).years + " Jahre"`

to calculate. The result is 24 Jahre (instead of 23 Jahre).

In order to verify, I put

`= (date(today) - this.birthday)`

which results in 24 years (instead of 23 years, and something).

Clearly a bug, I feel. Does this calculation "round" somehow? And if yes, can that be turned off to get exact results?


I have another table that shall show "upcoming birthdays in the next 2 months", with 3 columns File, Birthday, and Turns. This also shows that she’ll be turning 25 instead of 24.
Here’s the code:

table birthday as "Birthday", choice(
date(today).month*100+date(today).day = birthday.month*100+birthday.day,(date(today)-birthday).year,(date(today)-birthday).year+1) as Turns
from "People/Friends & Family"
where file.name != this.file.name and
choice(date(today).month*100+date(today).day > birthday.month*100+birthday.day,
(date(today).year+1)*10000 + birthday.month*100 + birthday.day,
date(today).year*10000 + birthday.month*100 + birthday.day) <= (date(today) + dur(2 months)).year*10000+(date(today) + dur(2 months)).month*100+(date(today) + dur(2 months)).day
sort choice(
date(today).month*100+date(today).day > birthday.month*100+birthday.day,
(date(today).year+1)*10000 + birthday.month*100 + birthday.day,
date(today).year*10000 + birthday.month*100 + birthday.day)

so I assume dataview really returns a "bad" (or rounded?) calculation result!

I’m (wildly) assuming you’re using some “forum-like-date-display” code here, where it might be okay to say “someone commented roughly 24 years ago” but I would prefer exact results when working with a database-like system. Otherwise we won’t get good and exact birthday-type calculations, right? 😉

Hint: If using moment.js, relative time thresholds can be customized: https://momentjs.com/docs/#/customization/relative-time-threshold/

@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented May 7, 2021

Apparently luxon rounds date differences internally (for what reason, I have no idea); I've updated date subtraction to produce exact durations.

blacksmithgu added a commit that referenced this issue May 7, 2021
- Better date subtraction calculation.
- `link(path, alias)` added
- Fix null value rendering
- Add `dv.current()`
@Moonbase59
Copy link
Author

@Moonbase59 Moonbase59 commented May 8, 2021

It’s probably for forum use, like in moment.js: The longer a datediff is, the less exact it is shown, i.e. not

3 years, 11 months, 30 days, 17 hours, 18 minutes ago

but

4 years ago

if its (I think) > 320 days into the year or so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants