# Count days from \_time (or \_value) to now

**URL:** <https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480>\
**Category:** Dashboards\
**Tags:** grafana\
**Created:** [March 31, 2022, 1:21pm UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480 "2022-03-31T13:21:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fernien](https://avatars.discourse-cdn.com/v4/letter/f/ea5d25/32.png) [@Fernien](https://community.influxdata.com/u/Fernien)\
**Post date:** [March 31, 2022, 1:21pm UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/1 "2022-03-31T13:21:53Z")

</div>

Hey needing your help again,

i want to know how many days have passed since the first database entry found. Currently i managed to get the \_time of the value i was searching for but i couldn’t find out how to calculate the days.

This is my Table and my Query. All i want is a calculation which tells me how many days have passed since 8th of march. Can someone help me? I’m stuck since days

 ![flux_again](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/3/3a1163f24e2fe4926d50d4950078ef4d926477db.png)

I was thinking about using duration() but couldnt get that to work either. If this works id like to display it in grafana which shouldnt be a problem. Calculation could also be done in grafana if that would work.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Juan\_Alonso\_Pla](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/juan_alonso_pla/32/9659_2.png) [@Juan\_Alonso\_Pla](https://community.influxdata.com/u/Juan_Alonso_Pla)\
**Post date:** [March 31, 2022, 5:01pm UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/2 "2022-03-31T17:01:52Z")

</div>

Hello @Fernien,

There is a function called events.duration(), which basically calculates the time elapsed between a record and the subsequent record but, for the final record, it compares with the time that you indicate. In this way, if you only have one record and specify now(), you will get the number of days passed in a new column.

I leave you here with the page of the function:

[events.duration() function | Flux 0.x Documentation (influxdata.com)](https://docs.influxdata.com/flux/v0.x/stdlib/contrib/tomhollingworth/events/duration/)

This is a code example of the function for your case:

|\> events.duration(  
unit: 1d,  
columnName: “duration”,  
timeColumn: “\_time”,  
stopColumn: “\_stop”,  
stop: now()  
)

And this is the output that you get. Notice the column _‘duration’_ with the number of days passed by.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/0/0a4a10a0617b405603b96d6417bfd706bfe05b87.png)

Let me know if this is what you were looking for.

---

<div class="post-metadata">

**Author:** ![Fernien](https://avatars.discourse-cdn.com/v4/letter/f/ea5d25/32.png) [@Fernien](https://community.influxdata.com/u/Fernien)\
**Post date:** [April 1, 2022, 8:50am UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/3 "2022-04-01T08:50:29Z")

</div>

OMG Thank you so so much!!!  
I spent soo much time trying different things but i seem to be to stupid for flux haha

Can’t thank you enough it works great!

---

<div class="post-metadata">

**Author:** ![Juan\_Alonso\_Pla](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/juan_alonso_pla/32/9659_2.png) [@Juan\_Alonso\_Pla](https://community.influxdata.com/u/Juan_Alonso_Pla)\
**Post date:** [April 1, 2022, 9:31am UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/4 "2022-04-01T09:31:01Z")

</div>

No worries! Happy to help 😁

---

<div class="post-metadata">

**Author:** ![PradoEagle](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@PradoEagle](https://community.influxdata.com/u/PradoEagle)\
**Post date:** [May 15, 2023, 8:59am UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/5 "2023-05-15T08:59:31Z")

</div>

Hi Juan\_Alonso\_Pia

I am trying to use the evets.duration to count the number of days but I can only get zero. I have the follow code extract.

import “contrib/tomhollingworth/events”  
from(bucket: “home”)  
|\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
|\> events.duration(unit: 1d)

This give the following

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/4/47e908a5bd00aca7f8d6d1663a98204bf1b58461.png)  
Any ideas why this giving 0 for duration?  
Thanks  
David

---

<div class="post-metadata">

**Author:** ![fercasjr](https://avatars.discourse-cdn.com/v4/letter/f/e495f1/32.png) [@fercasjr](https://community.influxdata.com/u/fercasjr)\
**Post date:** [May 15, 2023, 1:27pm UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/6 "2023-05-15T13:27:54Z")

</div>

at least that first page seems to be ok, the difference between second \_time entry and first \_time entry is 0 days, same with the third entry and the second entry, the elapsed time was 0 days

because those \_times were in the same day

---

<div class="post-metadata">

**Author:** ![PradoEagle](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@PradoEagle](https://community.influxdata.com/u/PradoEagle)\
**Post date:** [May 15, 2023, 10:23pm UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/7 "2023-05-15T22:23:37Z")

</div>

thanks for the reply. I thought that the events.duration provides the number of periods between the first and last record? i.e. the dataset.

---

<div class="post-metadata">

**Author:** ![fercasjr](https://avatars.discourse-cdn.com/v4/letter/f/e495f1/32.png) [@fercasjr](https://community.influxdata.com/u/fercasjr)\
**Post date:** [May 15, 2023, 10:39pm UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/8 "2023-05-15T22:39:01Z")

</div>

No, events duration gets the time between subsequent records. but there is a function for that, I just dont remember at the top of my mind.

Obtener [Outlook para Android](https://aka.ms/AAb9ysg)

---

<div class="post-metadata">

**Author:** ![fercasjr](https://avatars.discourse-cdn.com/v4/letter/f/e495f1/32.png) [@fercasjr](https://community.influxdata.com/u/fercasjr)\
**Post date:** [May 15, 2023, 10:47pm UTC](https://community.influxdata.com/t/count-days-from-time-or-value-to-now/24480/9 "2023-05-15T22:47:08Z")

</div>

Nvm i was thinking about spread() but it is to find the difference between min and max values, you could duplicate time, convert as int then use spread and convert the result to duration, but with all those steps maybe it will be easier just to get the cumulative sum of the result of events.duration()
