# Create a new field using painless

**URL:** https://discuss.elastic.co/t/create-a-new-field-using-painless/207295
**Category:** Elasticsearch
**Created:** [November 11, 2019, 8:01am UTC](https://discuss.elastic.co/t/create-a-new-field-using-painless/207295 "2019-11-11T08:01:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![maju3634](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@maju3634](https://discuss.elastic.co/u/maju3634)
#### Post date: [November 11, 2019, 8:01am UTC](https://discuss.elastic.co/t/create-a-new-field-using-painless/207295/1 "2019-11-11T08:01:46Z")

</div>

Hello i want to convert my Date of Birth ("DateBirth") data to be Age data using scripted field.  
So how it possible ? Can you tell me how to convert it ?

so this is my query:

![cek2](https://us1.discourse-cdn.com/elastic/original/3X/9/2/9241e79e57e64f41a6a84fb3d876df92ad67b252.png)

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [November 12, 2019, 12:42pm UTC](https://discuss.elastic.co/t/create-a-new-field-using-painless/207295/2 "2019-11-12T12:42:52Z")

</div>

Assuming that the `BirthDate` field has been mapped as a date field, the following should work:

```auto
GET my_index/_search
{
  "script_fields": {
    "age": {
      "script": {
        "source": "if (doc['BirthDate'].size() != 0) { ChronoUnit.YEARS.between(doc['BirthDate'].value, Instant.ofEpochMilli(System.currentTimeMillis()).atZone(ZoneId.systemDefault())) }"
      }
    }
  }
}

```

---

<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: [December 10, 2019, 12:42pm UTC](https://discuss.elastic.co/t/create-a-new-field-using-painless/207295/3 "2019-12-10T12:42:54Z")

</div>

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