# Calculate Age from DOB field

**URL:** https://discuss.elastic.co/t/calculate-age-from-dob-field/173647
**Category:** Kibana
**Created:** [March 24, 2019, 5:26pm UTC](https://discuss.elastic.co/t/calculate-age-from-dob-field/173647 "2019-03-24T17:26:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![LJB999](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@LJB999](https://discuss.elastic.co/u/LJB999)
#### Post date: [March 24, 2019, 5:26pm UTC](https://discuss.elastic.co/t/calculate-age-from-dob-field/173647/1 "2019-03-24T17:26:55Z")

</div>

Hi Can anyone advise how I can get Age from a dateofbirth datetime field to use in Kibana displays? I have created a couple of scripted fields to get DayofWeek and TimeofDay from another date field so I'm thinking this is the way to go but I'm not sure of the syntax for age?  
Guessing its to do with the difference between DOB and now(sysdate?)  
Thanks in advance

---

<div class="post-metadata">

### Author: ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)
#### Post date: [March 26, 2019, 6:52pm UTC](https://discuss.elastic.co/t/calculate-age-from-dob-field/173647/2 "2019-03-26T18:52:13Z")

</div>

We can calculate the number of years between two DateTime's using Java's Period class. To create a Period we'll turn the timestamp in the doc and the current time into two LocalDate objects. `doc['@timestamp'].value` returns a [JodaCompatibleZonedDateTime](https://snapshots.elastic.co/javadoc/org/elasticsearch/elasticsearch/6.7.0-SNAPSHOT/org/elasticsearch/script/JodaCompatibleZonedDateTime.html) which has a `toLocalDate` method. We can get a LocalDate representing the current time with `Instant.ofEpochMilli(System.currentTimeMillis()).atZone(doc['@timestamp'].value.getZone()).toLocalDate()`. Finally we can get the years from the Period object with the `getYears` method. Putting it all together we get:

```auto
return Period.between(
  doc['@timestamp'].value.toLocalDate(), 
  Instant.ofEpochMilli(System.currentTimeMillis()).atZone(doc['@timestamp'].value.getZone()).toLocalDate()          
).getYears()

```

---

<div class="post-metadata">

### Author: ![LJB999](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@LJB999](https://discuss.elastic.co/u/LJB999)
#### Post date: [March 27, 2019, 1:28pm UTC](https://discuss.elastic.co/t/calculate-age-from-dob-field/173647/3 "2019-03-27T13:28:41Z")

</div>

> [@Bargs](#):
>
> doc['@timestamp'].value.toLocalDate()

Thanks for the reply  
I tried to use this but am getting an error (I presume you meant in a scripted field?)

Even when just using timestamp I get this

```auto
"script": "return Period.between(\n doc['@timestamp'].value.toLocalDate(), \n Instant.ofEpochMilli(System.currentTimeMillis()).atZone(doc['@timestamp'].value.getZone()).toLocalDate() \n).getYears()",
    "lang": "painless",
    "caused_by": {
     "type": "illegal_argument_exception",
     "reason": "**Unable to find dynamic method [toLocalDate] with [0] arguments for class [org.joda.time.MutableDateTime**]."

```

however the field I actually want is called DOB so tried this too

It says There's an error in your script

```auto
{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "runtime error",
   "script_stack": [
    "return Period.between(\n doc['DOB'].value.toLocalDate(), \n Instant.ofEpochMilli(System.currentTimeMillis()).atZone(doc['DOB'].value.getZone()).toLocalDate() \n).getYears()",
    " ^---- HERE"

```

Is it something to do with my dates being datetime or the format of them?

timestamp looks like this

```auto
  "_id": "gnJMpGkBO7EWFsePkxo3",
  "myScriptedField": [
   "2019-03-22T07:27:53.042Z"
  ]

```

DOB like this

```auto
 "_id": "gnJMpGkBO7EWFsePkxo3",
  "myScriptedField": [
   "2004-01-17T23:00:00.000Z"
  ]

```

Many thanks

---

<div class="post-metadata">

### Author: ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)
#### Post date: [March 27, 2019, 2:49pm UTC](https://discuss.elastic.co/t/calculate-age-from-dob-field/173647/4 "2019-03-27T14:49:57Z")

</div>

What version of ES are you using?

---

<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: [April 24, 2019, 2:49pm UTC](https://discuss.elastic.co/t/calculate-age-from-dob-field/173647/5 "2019-04-24T14:49:58Z")

</div>

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