# Creating a new field through painless scripts to calculate the difference between two timestamp

**URL:** https://discuss.elastic.co/t/creating-a-new-field-through-painless-scripts-to-calculate-the-difference-between-two-timestamp/250345
**Category:** Kibana
**Tags:** painless
**Created:** [September 29, 2020, 11:17am UTC](https://discuss.elastic.co/t/creating-a-new-field-through-painless-scripts-to-calculate-the-difference-between-two-timestamp/250345 "2020-09-29T11:17:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Gerard\_Kamp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gerard_kamp/32/70139_2.png) [@Gerard\_Kamp](https://discuss.elastic.co/u/Gerard_Kamp)
#### Post date: [September 29, 2020, 11:17am UTC](https://discuss.elastic.co/t/creating-a-new-field-through-painless-scripts-to-calculate-the-difference-between-two-timestamp/250345/1 "2020-09-29T11:17:14Z")

</div>

Hi all,

i am trying to create a new field (time\_difference) in Kibana 7.4.2 to calculate the difference between two timestamp.

![image](https://us1.discourse-cdn.com/elastic/original/3X/5/d/5ded4e28abca9e8e68491e898c96d66f184f536c.png)

First i tried this

doc['@timestamp'].value - doc['p.dateTimeSend'].value

and i received  
Script is invalid. View script preview for details

i also tried:  
doc['@timestamp'].value.getMillis() - doc['p.dateTimeSend'].value.getMillis()

and i receive the same resut: Script is invalid. View script preview for details

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/3/03b4bfae9e593e1bc7ffe2c6ca33942b301cbaf9.png)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/0/f0537d12b5410bea74cb42fd9640a4f98bb60b51.png)

Please tell me what is wrong with my script.  
Thanks in advance!

---

<div class="post-metadata">

### Author: ![Gerard\_Kamp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gerard_kamp/32/70139_2.png) [@Gerard\_Kamp](https://discuss.elastic.co/u/Gerard_Kamp)
#### Post date: [September 30, 2020, 8:32am UTC](https://discuss.elastic.co/t/creating-a-new-field-through-painless-scripts-to-calculate-the-difference-between-two-timestamp/250345/2 "2020-09-30T08:32:59Z")

</div>

Hi everyone,

I think i find out why my script is invalid. It is because my second timestamp (p.dateTimeSend or payload.dateTimeSend).

When i execute only doc['@timestamp'].value.toInstant().toEpochMilli() it works but when i execute doc['p.dateTimeSend'].value.toInstant().toEpochMilli() or doc['payload.dateTimeSend'].value.toInstant().toEpochMilli() it don´t work.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/a/5a85b1d7116c7496ba1222c041c54253952cad9d.png)

Somebody know why payload.dateTimeSend is not working?  
how can i remove in all field the p. or payload. ?  
Thanks in advance!

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [October 5, 2020, 5:45pm UTC](https://discuss.elastic.co/t/creating-a-new-field-through-painless-scripts-to-calculate-the-difference-between-two-timestamp/250345/3 "2020-10-05T17:45:26Z")

</div>

Is the Payload field a nested field? Then that might be the issue.  
From what I see this should really work:  
`doc['@timestamp'].value.toInstant().toEpochMilli() - doc['utc_time'].value.toInstant().toEpochMilli() `

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [November 2, 2020, 5:45pm UTC](https://discuss.elastic.co/t/creating-a-new-field-through-painless-scripts-to-calculate-the-difference-between-two-timestamp/250345/4 "2020-11-02T17:45:33Z")

</div>

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