How to calculate the age of a note from YAML in years, months, days with dataview

I want to calculate the age of a note (in years, months, days) with dataview from YAML entries, (not from mdate or cdate). The perfect solution would be inline dataview so that I could write:

This file =this.file.name was last modified X years, Y months, Z days ago.

The fields ‘erstellt’ (created) und ‘geändert’ (modified) are defined by Linter plugin at YAML timestamp. The date format in Linter is

dddd, DD. MMMM YYYY, HH:mm:ss U\hr

The Date Format in the dataview settings are

dd. MMMM yyyy
ccc., dd. MMMM yyyy, HH:mm ‘Uhr’ '[KW’WW]

---
aliases: [Notiz]
tags: [Datum, Alter, Geburtstag, dataview, momentjs, luxon, Jahr, Monat, Tag, Stunde, Minute]
filename: dataview - Alter, Geburtstag, Datum
erstellt: Montag, 12. Juni 2023, 00:00:23 Uhr
geändert: Samstag, 07. Oktober 2023, 18:37:43 Uhr
heute: "`=date(today)`"
datum: "2023-06-12"
datum2: "`$=Date()`"
datum3: "`$=date()`"
datum4: "`$=Date('yyyy-MM-dd')`"
---

My dataview queries


```dataview
TABLE
date(today),
heute,
datum,
dateformat(datum, "yyyy-MM-dd"),
datum4,
date(today) - datum
where file.name = this.file.name
```

gives

List without id
date(today) - datum
where file.name = this.file.name

gives
image

Things I have tried

With cday (I don’t trust that cdate/mdate are not changed by accident in the long run.)

Diese Datei existiert seit =(date(today) - date(this.file.cday)).days Tagen.

With a fixed date in YAML (I don’t want to change YAML field every time to the current date.)

=(date(today) - date(this.datum)).days

Things that do not work:

=(date(today) - date(this.datum2)).days

=dateformat(datum, "yyyy-LLL-dd")

=dateformat(datum, "yyyy-MM-dd")

=(date(today) - date(this.datum2)).days

In recent version of dataview you’re able to use date(<some date string>, <date format>) to transform a date string into a proper date, however it’s not good when dealing with locale specific formats. However, if using moment() we can do some other magic, and that we can call from dataviewjs.

So try the following query in one of your files, and see if it makes sense, and if you can use it to your advantage:

```dataviewjs

const created = moment(dv.current().erstellt, "LLLL U\hr", "de")
const changed = moment(dv.current().geändert, "LLLL", "de")
const today = moment()

const changed_duration = moment.duration(today - changed)
const created_duration = moment.duration(today - created)

dv.paragraph("Created: " + created._d)
dv.paragraph("Changed: " + changed._d)
dv.paragraph("Today: " + today._d)

dv.paragraph("Since creation: " +
  Math.floor(created_duration.asDays()))
console.log(created_duration._data)

dv.paragraph("Since last change: " +
  changed_duration.humanize())
console.log(changed_duration._data)

```

The console.log() will show it’s data in the Console part of the Developer Tools pane, which I’m hoping you’re familiar with…

Do note that in general it’s better to use ISO dates to define dates all around, and then rather use date formatting when presenting the result after date calculations.

If your erstellt and geändert had been in ISO dates, you could’ve used ordinary DQL queries from dataview, and you would get the duration a lot easier than you’ve chosen to use locale specific date formats.

Thank you VERY much! Your code solves the problem:

Created: Mon Oct 02 2023 00:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)

Changed: Sat Oct 08 2022 18:37:00 GMT+0200 (Mitteleuropäische Sommerzeit)

Today: Sat Oct 07 2023 23:15:13 GMT+0200 (Mitteleuropäische Sommerzeit)

Since creation: 5 Tage

Since last change: ein Jahr

Do you have an idea to solve the mixed en/de locales in the output? (Saturday → Samstag, October → Oktober)

(Without knowing anything about JavaScript, I tried

var localLocale = moment();

moment.locale(‘de’); // default the locale to German
localLocale.locale(‘de’); // set this instance to use German
)

Stop - I found it. :blush:

```dataviewjs

var localLocale = moment();

moment.locale('de'); // Standardmäßig wird die Sprache auf Englisch gesetzt
localLocale.locale('de'); // Setzen Sie diese Instanz auf Deutsch

const created = moment(dv.current().erstellt, "LLLL", "de")

const changed = moment(dv.current().geändert, "LLLL", "de")
const today = moment()

const changed_duration = moment.duration(today - changed)

const created_duration = moment.duration(today - created)

dv.paragraph("Erstellt: " + created.format("LLLL") + " Uhr")  
dv.paragraph("Geändert: " + changed.format("LLLL") + " Uhr")
dv.paragraph("Heute: " + today.format("LLLL") + " Uhr")

dv.paragraph("Seit der Erstellung: " +
  Math.floor(created_duration.asDays()) + " Tage")
console.log(created_duration._data)

dv.paragraph("Seit der letzten Änderung: " +
  changed_duration.humanize())
console.log(changed_duration._data)
```


# Verkürzung (shorten)

```dataviewjs

const created = moment(dv.current().erstellt, "LLLL", "de")

const changed = moment(dv.current().geändert, "LLLL", "de")
const today = moment()

const changed_duration = moment.duration(today - changed)

const created_duration = moment.duration(today - created)

dv.paragraph("Seit der _Erstellung_ sind " + Math.floor(created_duration.asDays()) + " Tage vergangen," + " seit der _letzten Änderung_ " + changed_duration.humanize()+".")

```



This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.