Kibana, getting todays date

Hello there,

I want to calculate how many days past with my 'duedate' timestamp.
I guess I need a scripted field but I cannot get it right. Is there a way to this like
doc['today'].value - doc['duedate'].value ?
And it should give 30 (If 30 days passed from the duedate timestamp)

Hello,

You can use new Date().getTime() in scripted fields to get the current timestamp and use that in any operations there.

1 Like

Thanks for the reply. I did get todays date with your new Date().getTime()

But it shows me nothing when I try to get new Date().getTime() - doc['duedate'].getTime() and I tried representing it as date, string, number, nothing happens. And it doesnt give any compile or runtime errors?

Is duedate your date field? if so, you should use doc['duedate'].value

1 Like

Yes actually it is.

So what you are saying is I should try new Date().getTime() - doc['duedate'].value() ?

Yes. I think the getTime returns the date in the same numeric format as the one that's stored on ES.

1 Like

I tried that and I also did
(new Date().getTime()) - doc['duedate'].value()
Tried to represent as Number String and date nothing. Both give me 0 results on visualization. But I can get results for new Date().getTime()

I'm sorry, I confirmed your option without checking closely. You should do it without the parenthesis for value as that is a property and not a method for that doc type.
So it should look like this:
new Date().getTime() - doc['duedate'].value
Also, if you want to get a nice readable output for that field, you should set the Format to Number, the Type to Duration and the Input Format to milliseconds

1 Like

Perfect! Thank you ! Now everything looks perfect! Thanks for your time really.

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