# Formatting of dates returned by query

**URL:** <https://discuss.elastic.co/t/formatting-of-dates-returned-by-query/103746>\
**Category:** Elasticsearch\
**Created:** [October 12, 2017, 2:24pm UTC](https://discuss.elastic.co/t/formatting-of-dates-returned-by-query/103746 "2017-10-12T14:24:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markpavey](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@markpavey](https://discuss.elastic.co/u/markpavey)\
**Post date:** [October 12, 2017, 2:24pm UTC](https://discuss.elastic.co/t/formatting-of-dates-returned-by-query/103746/1 "2017-10-12T14:24:41Z")

</div>

The Elasticsearch documentation about the date data type says:

> Multiple formats can be specified by separating them with || as a separator. Each format will be tried in turn until a matching format is found. The first format will be used to convert the milliseconds-since-the-epoch value back into a string.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html#multiple-date-formats](https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html#multiple-date-formats)

If I do the following:

```
PUT date_mapping_test
{
  "mappings": {
    "date_test": {
      "properties": {
        "date": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}

PUT date_mapping_test/date_test/1
{
  "date": 1507804756000
}

PUT date_mapping_test/date_test/2
{
  "date": "2017-10-11"
}

PUT date_mapping_test/date_test/3
{
  "date": "2017-10-11 11:41:00"
}

GET date_mapping_test/date_test/_mapping

GET date_mapping_test/date_test/_search

```

Then I get the following response showing the date field values in the format that they were indexed.

```
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "date_mapping_test",
        "_type": "date_test",
        "_id": "2",
        "_score": 1,
        "_source": {
          "date": "2017-10-11"
        }
      },
      {
        "_index": "date_mapping_test",
        "_type": "date_test",
        "_id": "1",
        "_score": 1,
        "_source": {
          "date": 1507804756000
        }
      },
      {
        "_index": "date_mapping_test",
        "_type": "date_test",
        "_id": "3",
        "_score": 1,
        "_source": {
          "date": "2017-10-11 11:41:00"
        }
      }
    ]
  }

```

Whereas the documentation quoted above implies the dates should be formatted according to the first format in the list in the mapping, i.e. "yyyy-MM-dd HH:mm:ss".

Am I doing something wrong or misunderstanding the documentation, or is this a bug?

I am using Elasticsearch 5.5.1 on Windows.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 12, 2017, 2:39pm UTC](https://discuss.elastic.co/t/formatting-of-dates-returned-by-query/103746/2 "2017-10-12T14:39:35Z")

</div>

May it's unclear.

Elasticsearch never modifies the JSON document you sent. It's stored in `_source` field as is.

But if you store specifically the field `date` and then in `_search` ask for `stored_field` `date` instead of default `_source` you will get the converted value.

---

<div class="post-metadata">

**Author:** ![markpavey](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@markpavey](https://discuss.elastic.co/u/markpavey)\
**Post date:** [October 12, 2017, 3:08pm UTC](https://discuss.elastic.co/t/formatting-of-dates-returned-by-query/103746/3 "2017-10-12T15:08:54Z")

</div>

Thanks for the explanation, that makes sense.

---

<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:** [November 9, 2017, 3:09pm UTC](https://discuss.elastic.co/t/formatting-of-dates-returned-by-query/103746/4 "2017-11-09T15:09:24Z")

</div>

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