First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.
What I’m trying to do
I’m attempting to automatically calculate and display the age of wines in my Obsidian vault using Dataview (and potentially DataviewJS). Each wine note has a YAML frontmatter that includes a vintage
field which indicates the year the wine was made. My goal is to have a table, or a display in some format, which will show the age of the wine by subtracting the vintage
year from the year the note was created.
Once calculated, I’d ideally like this information to automatically populate a new YAML field called Wine Age🍷
.
For example, if a note was created in 2023 and the wine’s vintage
is 2018, the Wine Age🍷
should be populated with the value 5
.
Here’s a sample of my YAML frontmatter for context:
yamlCopy code
---
created: 15-09-23
Topics: Wine Diary
type: red
winename: Faiveley Pommard 1er Cru Les Rugiens
vintage: 2018
producer: Domaine Faiveley
subregion: Cote De Beaune
wine age:
---
Things I have tried
Things I have tried
- Dataview Query: I’ve tried querying the data using a standard Dataview block to display the age, but couldn’t figure out a way to perform arithmetic operations on the
vintage
field within Dataview. - DataviewJS Script: Based on a recommendation, I attempted to use DataviewJS to achieve this by calculating the difference between the year the note was created and the
vintage
year. Here’s the script I tried:
markdownCopy code
```dataviewjs
const noteCreatedYear = new Date(dv.current().created).getFullYear();
const wines = dv.pages() // Adjusted the path/query accordingly
.where(w => w.vintage)
.mutate(w => w.wineAge = noteCreatedYear - w.vintage);
dv.table(["Wine Name", "Vintage", "Wine Age🍷"],
wines.map(w => [w.winename, w.vintage, w.wineAge]))
However, this script didn’t produce the expected results! Any help greatly appreciated