Subtracting dates in dataview returns float numbers

I am trying to make a countdown where it shows me the remaining days, when subtracting between two dates it gives me very long numbers, I only want it to show me 2 digits.

ViewMode:

SourceMode:

Does it work if the .hour is removed?

---
Added: 2023-03-28T05:50
Ended: 2023-03-28T12:47
Tags: atestnote2
---


```dataview
TABLE
Added,
Ended,
(Ended - date(now)) AS Left
from #atestnote2
```

1 Like

Reading and thinking about your question in more detail, I can see that my answer above misses the point.

I can’t get the kind of restriction you want in the output. Don’t know if that’s down to all the wrong things I have tried or whether Dataview is throwing an error in the calculation.

Hope someone else can help.

EDIT: Rounding appears to help… though I think it shouldn’t be necessary.

---
Added: 2023-03-28T05:50
Ended: 2023-03-29T17:55
Tags: atestnote2
---

```dataview
TABLE
Added,
Ended,
round(dur(Ended - date(now)).hours) AS HoursRemaining
from #atestnote2
```

*** 
# hours
`= (this.Ended - date(now)).hours` hours
***

1 Like

Thanks eightning!!!

1 Like

@anon63144152 beat me to provide a solution related to round(), so I just want to add that this function is indeed rounding the answer you’ve got, and not just truncating the hours.

Depending on your preferences, that could be exactly what you want (or not), but if you want something closer to truncating the decimal part, you could try something like: round(dur(this.Ended - date(now)).hours - 0.5) which kind of cancels out the rounding effect.

As of now, there is no trunc() available in dataview, so we’ll have to resort to this kind of trickery to achieve truncation, if that’s the wanted effect.

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