Duration from a specific start date

The table below is using a duration query to work out the amount of time elapsed since a specific start date: 2021-10-19

When parsed, the query says 15 November is 3 weeks and 6 days since the start date.

It then says that 16 November is 1 month since the start date.

It then says that 17 November is 1 month and 1 day since the start date.

It then says that 18 November is 1 month since the start date.

It then says that 19 November is also 1 month since the start date.

It then says that 20 November is 1 month and 1 day since the start date.

Duration

Date Duration
2021-11-15 =date(2021-11-15) - date(2021-10-19)
2021-11-16 =date(2021-11-16) - date(2021-10-19)
2021-11-17 =date(2021-11-17) - date(2021-10-19)
2021-11-18 =date(2021-11-18) - date(2021-10-19)
2021-11-19 =date(2021-11-19) - date(2021-10-19)
2021-11-20 =date(2021-11-20) - date(2021-10-19)

Here is the table, ready to copy and paste into an Obsidian file:

**Date** | **Duration**
:------------ | :------------
2021-11-15 | `=date(2021-11-15) - date(2021-10-19)`
2021-11-16 | `=date(2021-11-16) - date(2021-10-19)`
2021-11-17 | `=date(2021-11-17) - date(2021-10-19)`
2021-11-18 | `=date(2021-11-18) - date(2021-10-19)`
2021-11-19 | `=date(2021-11-19) - date(2021-10-19)`
2021-11-20 | `=date(2021-11-20) - date(2021-10-19)`

Possible to resolve this?

Thanks

Angel

Hi.
I don’t know any way to define the duration output format.
I only found a way (via javascript - dvjs) to define the output only in “days”:

inline dvjs

`$=dv.paragraph(moment('2021-11-16').diff(moment('2021-10-19'), 'days') + " days")`

Maybe there are other ways, but my JS knowledge is almost zero.

1 Like

Thank you. That suits me well as I would actually rather have days than years, months, and days.

I tweaked the code, for aesthetic reasons only, to use an array rather than a paragraph as an array matches my journal’s design:

`$=dv.array(moment('2021-11-15').diff(moment('2021-10-19'), 'days') + " days")`

For my journal template, I am using the following code to create the day’s date dynamically:

`$=dv.array(moment('<% tp.date.now("YYYY-MM-DD") %>').diff(moment('2021-10-19'), 'days') + " days")`

Here’s a sample of your code working for different dates. Perfection:

**Date** | **Duration**
:------------ | :------------
2021-11-15 | `$=dv.array(moment('2021-11-15').diff(moment('2021-10-19'), 'days') + " days")`
2021-11-16 | `$=dv.array(moment('2021-11-16').diff(moment('2021-10-19'), 'days') + " days")`
2021-11-17 | `$=dv.array(moment('2021-11-17').diff(moment('2021-10-19'), 'days') + " days")`
2021-11-18 | `$=dv.array(moment('2021-11-18').diff(moment('2021-10-19'), 'days') + " days")`
2021-11-19 | `$=dv.array(moment('2021-11-19').diff(moment('2021-10-19'), 'days') + " days")`
2021-11-20 | `$=dv.array(moment('2021-11-20').diff(moment('2021-10-19'), 'days') + " days")`
2021-12-09 | `$=dv.array(moment('2021-12-09').diff(moment('2021-10-19'), 'days') + " days")`

I am grubbing around in the dark with all of this, but I saw your comment on GitHub. Is this what you mean?

https://blacksmithgu.github.io/obsidian-dataview/query/expressions/#durations

Indebted. Wowed. And very grateful.

Angel

Hi. Not exactly, because with expressions like duration.months or duration.days what you get it’s not a (total) conversion to months or days but an extraction of the months or days.
E.g., in your example above for 2021-11-17 you have as output “1 months, 1 days”. If you use .month you get “1” and if you use .days you get “1” (not the conversion of the total in days).

1 Like

Buongiorno.

Ah, I partly understand.

Now trying to see if I can find a way to format the number of days to include a comma as a separator for thousands. More grubbing for me.

Un milione di grazie. Ciao.

Angel

(I understand some italian because I need to read a lot in this language for my academic research, but I’m portuguese :slight_smile: )

About this, I found this expression: toLocaleString(). If you use .toLocaleString('en-US') you get a (“,”), if you use .toLocaleString(‘de-DE’)` you get a (“.”).

`$=dv.paragraph(moment('2021-11-16').diff(moment('2015-11-01'), 'days').toLocaleString('en-US') + " days")`
1 Like

Ah, a soul from the Iberian Peninsula. Please, forgive my assumption. I had seen you write in Italian, so …

Clearly, you are an accomplished polyglot.

I had just got to the point of finding the locale string, but not managed to place it in the right part of your code to make it actually work.

Thank you, yet again, for sharing your knowledge and time. Super grateful.

Um milhão de agradecimentos e desculpas.

Angel

1 Like

Don’t need to apologize :slight_smile:
Just a language clarification: in portuguese the “thanks” expression is ‘almost’ similar to italian (“grazie mille”) but more similar to english - we don’t use “mille” (thousands) as synonym to “much” but the true “much” (“muito”).
Thank you very much = Muito obrigado

Muito obrigado (there is no language that I cannot wreck … unfortunately).

Boa tarde!

Angel

1 Like

Maybe one day we can be experts in javascript language - a language from Java planet!

Out of the two of us, only one has any chance of ever being an expert in JavaScript … and it certainly won’t be me. I’ll be the expert in wrecking it.

:angel:

1 Like

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