# Access the epoch of the date type doc in groovy script

**URL:** https://discuss.elastic.co/t/access-the-epoch-of-the-date-type-doc-in-groovy-script/53129
**Category:** Elasticsearch
**Created:** [June 17, 2016, 8:57am UTC](https://discuss.elastic.co/t/access-the-epoch-of-the-date-type-doc-in-groovy-script/53129 "2016-06-17T08:57:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dhruvpsaru](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@dhruvpsaru](https://discuss.elastic.co/u/dhruvpsaru)
#### Post date: [June 17, 2016, 8:57am UTC](https://discuss.elastic.co/t/access-the-epoch-of-the-date-type-doc-in-groovy-script/53129/1 "2016-06-17T08:57:02Z")

</div>

As per my understanding and from here [[http://stackoverflow.com/questions/29341715/understanding-how-elasticsearch-stores-dates-internally](http://stackoverflow.com/questions/29341715/understanding-how-elasticsearch-stores-dates-internally)] ([http://stackoverflow.com/questions/29341715/understanding-how-elasticsearch-stores-dates-internally](http://stackoverflow.com/questions/29341715/understanding-how-elasticsearch-stores-dates-internally)) ealstic search stores the date internally as epoch format. Now consider I need to access this epoch in groovy script and out doc date format is date\_optional\_time. Now when I try to access it in groovy script it gives me the formatted date(as on the time of input). Is there a way to access the epoch time here.  
I have come up with three thoughts

1. Convert the doc value to date and get millis in script,
2. Create a new field with copy\_to that stores the date as epoch format  
3)//or if possible directly access the epoch. but how?

Can some body guide me on this.I need epoch because I need to update some other field on basis of the epoch

e.g  
Consider a mapping like this  
{  
"createdDate": {  
"type": "date",  
"store": true,  
"format": "dateOptionalTime"  
}  
"modifiedDate": {  
"type": "date",  
"store": true,  
"format": "dateOptionalTime"  
}  
"daysINBetween" : {  
"type" : "long"  
}  
,  
}

Now I need to run a script that stores `(createdDate.millis - modifiedDate.millis) / (24 * 60 * 60 * 1000)`. I don't want to create a new object of date each time, that's why I am trying to access epoch in script

[http://stackoverflow.com/questions/37877534/access-epoch-from-date-field-in-groovy-script-in-elastic-search](http://stackoverflow.com/questions/37877534/access-epoch-from-date-field-in-groovy-script-in-elastic-search)

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [June 17, 2016, 12:45pm UTC](https://discuss.elastic.co/t/access-the-epoch-of-the-date-type-doc-in-groovy-script/53129/2 "2016-06-17T12:45:10Z")

</div>

Hey,

try `doc['createdDate'].date.getMillis()`

I would try to do calculate this on index time, instead of query time though, in order to have faster queries.

--Alex

---

<div class="post-metadata">

### Author: ![dhruvpsaru](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@dhruvpsaru](https://discuss.elastic.co/u/dhruvpsaru)
#### Post date: [June 17, 2016, 1:18pm UTC](https://discuss.elastic.co/t/access-the-epoch-of-the-date-type-doc-in-groovy-script/53129/3 "2016-06-17T13:18:58Z")

</div>

> [@spinscale](#):
>
> doc['createdDate'].date.getMillis()

As per my knowledge `doc['createdDate'].date.getMillis()` will be accessible in script\_field only.Am I right?

---

<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: [July 5, 2017, 10:42pm UTC](https://discuss.elastic.co/t/access-the-epoch-of-the-date-type-doc-in-groovy-script/53129/4 "2017-07-05T22:42:46Z")

</div>


