What I’m trying to do
I have notes on people and in the front matter I have a birthday
property with a date formatted YYYY-MM-DD
Carla’s birthday is 1993-03-17 which makes her 30. I would like a section under the front matter with a simple dataview query using momentjs (or anything else) to calculate her age based on the front matter.
Carla’s Note
---
birthday: 1993-03-17
---
Age:: 30
Sorry for the code dump but but after many attempts at what I feel should work and searching StackOverflow, Reddit, and this form. I don’t know what I am doing wrong.
I would prefer the inline Dataview so that it can come after the Age::
Things I have tried
This is driving me insane, this should be a simple currentDate
- birthday
. But no matter what I did, I could not get anything close to the correct answer, or it just throw NaN
, -NaN
, <$>
`$= moment().diff(moment(dv.current().birthday, "YYYY-MM-DD"), 'years')`
Produces:
NaN
`$= Math.floor(moment(new Date()).diff(moment(dv.current().birthday,"YYYY-MM-DD"),'years',true))`
Produces:
NaN
`$= moment().subtract(dv.current().birthday, 'year').format('YY')`
Produces:
-NaN
`$= moment().endOf('day').diff(moment(dv.current().birthday, 'MMMM DD[,] YYYY'), 'years')`
Produces:
NaN
`$= moment().endOf('day').diff(1993, 'years')`
Produces:
53
(How, why, What?)
`$= moment(moment().endOf('day').format('YY')).subtract(dv.current().birthday, 'years')`
Produces:
<$>
`$= moment().subtract(moment(dv.current().birthday).format('YYYY'), 'years').format('YY')`
Produces:
00
(Why?)
```dataviewjs
const birthday = moment(dv.current().birthday).format('YYYY-MM-DD');
dv.paragraph("birthday: " + birthday);
const parsedBirthday = moment(birthday).format('YYYY');
dv.paragraph("parsedBirthday: " + parsedBirthday);
const ageInYears = moment().diff(parsedBirthday, 'years');
dv.paragraph("ageInYears: " + ageInYears);
const ageAsTwoDigitYear = moment().subtract(ageInYears, 'years').format('YY');
dv.paragraph("ageAsTwoDigitYear: " + ageAsTwoDigitYear);
```
Produces:
birthday: 2023-04-18
parsedBirthday: 2023
ageInYears: 0
ageAsTwoDigitYear: 23
Not even remotly close…