# Substract two time fields in index pattern

**URL:** https://discuss.elastic.co/t/substract-two-time-fields-in-index-pattern/311580
**Category:** Elasticsearch
**Tags:** painless, runtime-fields
**Created:** [August 6, 2022, 11:00am UTC](https://discuss.elastic.co/t/substract-two-time-fields-in-index-pattern/311580 "2022-08-06T11:00:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Farid\_N](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/farid_n/32/74144_2.png) [@Farid\_N](https://discuss.elastic.co/u/Farid_N)
#### Post date: [August 6, 2022, 11:00am UTC](https://discuss.elastic.co/t/substract-two-time-fields-in-index-pattern/311580/1 "2022-08-06T11:00:59Z")

</div>

Hello dears

I have an instance of heartbeat that check all of certificates of my websites. every log looks like this:

```auto
{
  "_index": "heartbeat-7.16.2-2022.08.01-000001",
  "_type": "_doc",
  "_score": 1,
  "_source": {
    "@timestamp": "2022-08-06T10:47:49.130Z",
    "ip": "My IP",
    "url": {
      "scheme": "https",
      "domain": "my.web.site",
      "port": 443,
       }
    "tls": {
      "certificate_not_valid_after": "2022-10-19T09:39:32.000Z",
      "established": true,
     }
  }
}

```

I want to create a field in heartbeat-\* `index pattern` in `Scripted fields` to create a new field with name `remaining_time` which is day reminder of certificate validation.

All I want is a script that substract `@timestamp` with `certificate_not_valid_after` field so I can have remaining time for my certificates.

My problem is, how should I write this `script` in `Scripted fields` in order to create a new field?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/4/74ed083d0e8014301f98936035e8b2d0e2b665f9.png)

I'm using Elasticstack version 7.16.2

Thank you all

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [August 9, 2022, 1:16am UTC](https://discuss.elastic.co/t/substract-two-time-fields-in-index-pattern/311580/2 "2022-08-09T01:16:05Z")

</div>

You would be better off using [Runtime fields | Elasticsearch Guide [7.16] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.16/runtime.html) instead of scripted fields.

Something like this should work;

```auto
if (!doc['field1'].empty && !doc['field2'].empty) {
  return ((doc['field1'].value - doc['field2'].value.millis) / 1000);
} else {
  return 0;
}

```

---

<div class="post-metadata">

### Author: ![Farid\_N](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/farid_n/32/74144_2.png) [@Farid\_N](https://discuss.elastic.co/u/Farid_N)
#### Post date: [August 10, 2022, 12:15pm UTC](https://discuss.elastic.co/t/substract-two-time-fields-in-index-pattern/311580/3 "2022-08-10T12:15:38Z")

</div>

Thank you so much

I have got the require information from "Uptime" observability

---

<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: [September 7, 2022, 2:16pm UTC](https://discuss.elastic.co/t/substract-two-time-fields-in-index-pattern/311580/4 "2022-09-07T14:16:32Z")

</div>

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