Dataviewjs only number of days

What I’m trying to do

Hello,
I try to get the number of days between two dates via dataviewjs.

Things I have tried

---
enddate: 2023-08-15
---
'$=dv.span(dv.func.date(dv.current().enddate).diff(dv.date('today'),'days'))'

gives me
“3 weeks” → grafik

How can I get that I only have the number of days like
21 ?

You don’t need the dv.func.date part. You can get the days like this:

`$=dv.span(dv.current().enddate.diff(dv.date('today'),'days').days)`
1 Like

Thank you very much, Alan!

is there a way that changes in the metadata (e.g. date ) will be right executed in the inline DJVS, or is this only working in reading mode? And I have to click in the DVJS line to see this changes?

well, I will go with the DVJS block…

but how can I add/subtract a number of days to the example from above,
when I have another metadata field

daysBuffer:: 21 days

```dataviewjs
dv.span(dv.current().enddate.diff(dv.date('today'),'days').days) - dv.current().daysbuffer
```

I get something like this:
-1814399953
I dont know, how I convert this again in days again…?

edit:

well, my miserable attempt…

the formula

```dataviewjs
dv.span(dv.current().enddate.diff(dv.date('today'),'days') - dv.current().daysBuffer )
```

what brings:
2246400000

I tried to insert this into an expression of
dv.func.dateformat(xx, "yyyy-MM-dd")

for the sake of clarity I set it into a variable varStrDat

```dataviewjs
let varStrDat = dv.current().enddate.diff(dv.date('today'),'days') - dv.current().daysBuffer;
dv.span(dv.func.dateformat(varStrDat, "yyyy-MM-dd"))	   
```

but woe and ah:

Evaluation Error: Error: No implementation of 'dateformat' found for arguments: number, string
    at self (plugin:dataview:13146:19)
    at Object.eval [as dateformat] (plugin:dataview:13156:29)
    at eval (eval at <anonymous> (plugin:dataview), <anonymous>:1:55)
    at DataviewInlineApi.eval (plugin:dataview:18404:16)
    at evalInContext (plugin:dataview:18405:7)
    at asyncEvalInContext (plugin:dataview:18415:32)
    at DataviewJSRenderer.render (plugin:dataview:18436:19)
    at DataviewRefreshableRenderer.maybeRefresh (plugin:dataview:18014:22)
    at e.tryTrigger (app://obsidian.md/app.js:1:906862)
    at e.trigger (app://obsidian.md/app.js:1:906795)

someone got a hint? I’m getting friable…

edit_2:

next try via moment()

```dataviewjs
let varStrDat = dv.current().enddate
	 .diff(dv.date('today'),'days') - dv.current().daysBuffer;
dv.span(moment(varStrDat).format("YYYY-MM-DD"))
```

output: 1970-01-27
that cant be the right date! not at this value of daysBuffer:: 21 days

You can use moment() without any args to get a moment date object for today

Currently using a script to give me number of days until payday.

const endDate = moment("2023-08-15");
const days = Math.abs(moment().diff(endDate,"days")) + 1; 
// plus one as you want to count the current day
dv.paragraph(`${days} days until endDate`);

oh, thank you!
but when I put my values into your code:

```dataviewjs
const endDate = moment(dv.current().enddate);
const days = Math.abs(moment().diff(endDate,"days")) - dv.current().daysBuffer; 
// subtract the buffer days - here: 21 days
dv.paragraph(`${days} days until endDate`);
```

I get this:
-1814400000 days until endDate

Ahh. It looks like when you have a date format in your frontmatter. Dataview interprets it as a dv date object. Sorry I’m not familiar with those as well as moment, you just need to pass something in that moment can infer as a date. Timestamp works

dv.current().enddate.ts

const endDate = moment(dv.current().enddate.ts);
const days = Math.abs(moment().diff(endDate,"days")) - dv.current().daysBuffer;
// subtract the buffer days - here: 21 days
dv.paragraph(`${days} days until endDate`);

Edit: Sorry ignore my console logs :sweat_smile:

thanks for your efforts and patience.

but unfortunately it helps not:

but the value is different already.
I got:
-1814399954 days until endDate

Hmm. That is strange. Are you sure it’s getting the front matter okay? Maybe try logging the dv.current().end date out?

Edit: I notice in your messages your daysBuffer has two colons?

Is that a typo?

It should be

daysBuffer: 21

the enddate is right in the YAML-Fontmatter,
but the daysBuffer is beneth YAML

---
enddate: 2023-08-15
---
daysBuffer:: 21 days

Ahh, I’ve not seen that :: syntax outside of the frontmatter. Useful :smiley:

I see also that you’re putting 21 days meaning that dv is inferring it as a duration type. You need to extract the days number from that.

dv.current().daysBuffer.days

const endDate = moment(dv.current().enddate.ts);
const days = Math.abs(moment().diff(endDate,"days")) - dv.current().daysBuffer.days;
// subtract the buffer days - here: 21 days
dv.paragraph(`${days} days until endDate`);

Edit:

Going back to your dv.date one liner. You’re subtracting two duration types, so you just need to pull out the days from them

dv.span(dv.current().enddate.diff(dv.date('today'),'days').days - dv.current().daysBuffer.days)
const endDate = moment(dv.current().enddate.ts);
const days = Math.abs(moment().diff(endDate,"days")) - dv.current().daysBuffer.days;
// subtract the buffer days - here: 21 days
dv.paragraph(`${days} days until endDate`);

that give the line:
46 days until endDate
but that can’t be right.
i took a real enddate of my project
2023-09-10

less 21 days results 2023-08-20

so there have to be
26 days until endDate

and not 46 days

maybe somewhere I got a thinking error…

Try the new onliner with the days extracted from the duration. I think my Math.abs in my code is making positive somewhere you need a negative

dv.span(dv.current().enddate.diff(dv.date('today'),'days').days - dv.current().daysBuffer.days)

it says even one more:
47

It’s 47 days until your end date. So it sounds like it’s not picking up the daysBuffer correctly

Have you kept the format

---
enddate: 2023-08-15
---
daysBuffer:: 21 days

With the value

---
enddate: 2023-08-15
---

I get:
20

but as I wrote above,
If the date is more far in future, the calculation is wrong.
I used another date from my real projects, where I know the differences

with the date
2023-09-10
I get:
46
what should have been
25

so 2023-09-10
minus daysbuffer 21 days
2023-08-20
2023-08-20 until today → 25 days

Dataview turns something like 21 days into a Duration, which makes everything very easy.

Full version:

```dataviewjs
const endDate = dv.current().enddate
const daysBetween = endDate.diffNow()
const subtractTheBuffer = daysBetween.minus(dv.current().daysBuffer)
// Output the final value as the total amount of days
dv.span(subtractTheBuffer.as('days'))
```

One-liner:

`$=dv.current().enddate.diffNow().minus(dv.current().daysBuffer).as('days')`

And here’s the full note text for the above so you can copy/paste:

---
enddate: 2023-09-10
---
daysBuffer:: 21 days

`$=dv.current().enddate.diffNow().minus(dv.current().daysBuffer).as('days')`

That’s assuming you want to subtract your date buffer. If you want to add it, just change .minus in the code to .plus


edit: I see you do want to subtract. So using your date from your previous post:

---
enddate: 2023-09-10
---
daysBuffer:: 21 days

`$=dv.current().enddate.diffNow().minus(dv.current().daysBuffer).as('days')`

Output is 24.3 days.

1 Like

Thank you Alan, again.
that looks very clerly arranged.
But the results are:

the oneliner
40.38345101851852

DVJS-Block
40.38345099537037

I’ve tried another date in enddate, what goes beyond 1 month (2023-09-10)

oneliner:
66.38130334490741
DVJS-Block:
66.38162221064815

Does DVJS gives a numeric value back?

… so 2023-09-10
minus daysbuffer 21 days
2023-08-20
2023-08-20 until today → 25 days

I don’t think you have read my post :wink: It specifically asked if you wanted to add or subtract the buffer, and gave you instructions on what you needed to adjust depending on your preference.

I have now edited it to subtract the buffer as I see from your previous post that this is what you wanted.

Just copy and paste this:

---
enddate: 2023-09-10
---
daysBuffer:: 21 days

`$=dv.current().enddate.diffNow().minus(dv.current().daysBuffer).as('days')`

Output will be 24.3 days.

1 Like

sorry Alan, I was just about to try it, but then I saw your edit already.

well, how do I get this cryptical number
24.364409895833333
to an integer of 24?
the round() funbction doest work here.