# Add a custom field in Kibana

**URL:** <https://discuss.elastic.co/t/add-a-custom-field-in-kibana/322138>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [December 29, 2022, 7:20am UTC](https://discuss.elastic.co/t/add-a-custom-field-in-kibana/322138 "2022-12-29T07:20:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jessutanto](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jessutanto](https://discuss.elastic.co/u/jessutanto)\
**Post date:** [December 29, 2022, 7:20am UTC](https://discuss.elastic.co/t/add-a-custom-field-in-kibana/322138/1 "2022-12-29T07:20:27Z")

</div>

Hi,

I wanted to add a field where it calculates the age of a person, thus --\> Birth date - Now

I tried to implement [this guide](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-datetime.html). [see the **Datetime Now Example**].

1. Somehow I got an error said **cannot resolve symbol [zdt]**.

![Screenshot 2022-12-29 at 08.19.59](https://us1.discourse-cdn.com/elastic/original/3X/f/a/fa386461ea63212428c678a766cc89c508260d4a.png)

Then, I suspect that zdt is the brief term for ZonedDateTime. I changed the zdt to ZonedDateTime, then I got an error said **static method [java.time.ZonedDateTime, toInstant/0] not found**. Idk what's wrong here?  
 ![Screenshot 2022-12-29 at 08.19.21](https://us1.discourse-cdn.com/elastic/original/3X/a/2/a2121f1176650af8071f7624756eb53d9fc9bbf9.png)

1. Do we need to define the parameter "now" as in the **Input**? Where do we do that? How?

Please kindly advise. Thanks!

---

<div class="post-metadata">

**Author:** ![jsanz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsanz/32/53734_2.png) [@jsanz](https://discuss.elastic.co/u/jsanz)\
**Post date:** [December 29, 2022, 5:55pm UTC](https://discuss.elastic.co/t/add-a-custom-field-in-kibana/322138/2 "2022-12-29T17:55:14Z")

</div>

In your first try you it was complaining because in line 2 you named the variable `lastScraped` then used `zdt` in line 3.

Check the code below, you can run it on the Kibana Dev tools and later move the source of the script to your data view.

```auto
# Create a testing index
PUT discuss-322138
{
  "settings": {
    "number_of_replicas": 1
  }, 
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "birthDate": { "type": "date"}
    }
  }
}

# Add some data
POST discuss-322138/_bulk
{ "index": {}}
{ "name": "john", "birthDate": "1965-12-29"}
{ "index": {}}
{ "name": "mary", "birthDate": "1985-12-29"}

# Search with a runtime field
GET discuss-322138/_search
{
  "fields": [
    "age"
  ], 
  "runtime_mappings": {
    "age": {
      "type": "long",
      "script": {
        "source": """
        Instant now = Instant.ofEpochMilli(new Date().getTime());
        ZonedDateTime zdtNow = ZonedDateTime.ofInstant(now, ZoneId.of('Z'));
        ZonedDateTime zdtBirth = doc['birthDate'].value;
        
        emit(ChronoUnit.YEARS.between(zdtBirth, zdtNow))
        """
      }
    }
  }
}

```

---

<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:** [January 26, 2023, 5:55pm UTC](https://discuss.elastic.co/t/add-a-custom-field-in-kibana/322138/3 "2023-01-26T17:55:19Z")

</div>

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