# Format date in elasticsearch query (during retrieval)

**URL:** <https://discuss.elastic.co/t/format-date-in-elasticsearch-query-during-retrieval/21615>\
**Category:** Elasticsearch\
**Created:** [January 13, 2015, 8:56pm UTC](https://discuss.elastic.co/t/format-date-in-elasticsearch-query-during-retrieval/21615 "2015-01-13T20:56:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hendrik\_Dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_dev/32/978_2.png) [@Hendrik\_Dev](https://discuss.elastic.co/u/Hendrik_Dev)\
**Post date:** [January 13, 2015, 8:56pm UTC](https://discuss.elastic.co/t/format-date-in-elasticsearch-query-during-retrieval/21615/1 "2015-01-13T20:56:36Z")

</div>

I have a elasticsearch index with a field "aDate" (and lot of other fields)  
with the following mapping

"aDate" : {  
"type" : "date",  
"format" : "date\_optional\_time"}

When i query for a document i get a result like

"aDate" : 1421179734000,

I know this is the epoch, the internal java/elasticsearch date format, but  
i want to have a result like:

"aDate" : "2015-01-13T20:08:54",

I play around with scripting

{  
"query":{  
"match\_all":{

}  
},  
"script\_fields":{  
"aDate":{  
"script":"if (!\_source.aDate?.equals('null')) new java.text.SimpleDateFormat('yyyy-MM-dd\'T\'HH:mm:ss').format(new java.util.Date(\_source.aDate));"  
}  
}}

but it give strange results (script works basically, but aDate is the only  
field returned and \_source is missing). This looks like

"hits": [{  
"\_index": "idx1",  
"\_type": "type2",  
"\_id": "8770",  
"\_score": 1.0,  
"fields": {  
"sentDate": ["2015-01-12T17:15:47"]  
}  
},

I would prefer a solution without scripting if possible.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/4c2d9986-1ba7-4161-b633-4df9cfd16b10%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4c2d9986-1ba7-4161-b633-4df9cfd16b10%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Hendrik\_Dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_dev/32/978_2.png) [@Hendrik\_Dev](https://discuss.elastic.co/u/Hendrik_Dev)\
**Post date:** [January 14, 2015, 3:19pm UTC](https://discuss.elastic.co/t/format-date-in-elasticsearch-query-during-retrieval/21615/2 "2015-01-14T15:19:07Z")

</div>

answer is  
here [java - Format date in elasticsearch query (during retrieval) - Stack Overflow](http://stackoverflow.com/questions/27931241/format-date-in-elasticsearch-query-during-retrieval/27932753)

Am Dienstag, 13. Januar 2015 21:56:36 UTC+1 schrieb Hendrik Dev:

> I have a elasticsearch index with a field "aDate" (and lot of other  
> fields) with the following mapping
> 
> "aDate" : {  
> "type" : "date",  
> "format" : "date\_optional\_time"}
> 
> When i query for a document i get a result like
> 
> "aDate" : 1421179734000,
> 
> I know this is the epoch, the internal java/elasticsearch date format, but  
> i want to have a result like:
> 
> "aDate" : "2015-01-13T20:08:54",
> 
> I play around with scripting
> 
> {  
> "query":{  
> "match\_all":{
> 
> }  
> },  
> "script\_fields":{  
> "aDate":{  
> "script":"if (!\_source.aDate?.equals('null')) new java.text.SimpleDateFormat('yyyy-MM-dd\'T\'HH:mm:ss').format(new java.util.Date(\_source.aDate));"  
> }  
> }}
> 
> but it give strange results (script works basically, but aDate is the only  
> field returned and \_source is missing). This looks like
> 
> "hits": [{  
> "\_index": "idx1",  
> "\_type": "type2",  
> "\_id": "8770",  
> "\_score": 1.0,  
> "fields": {  
> "sentDate": ["2015-01-12T17:15:47"]  
> }  
> },
> 
> I would prefer a solution without scripting if possible.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a6633b58-6b3c-46df-b0fa-373c5f73bb89%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a6633b58-6b3c-46df-b0fa-373c5f73bb89%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:38am UTC](https://discuss.elastic.co/t/format-date-in-elasticsearch-query-during-retrieval/21615/3 "2017-07-06T00:38:58Z")

</div>


