# How to query DataOptionaTime field

**URL:** <https://discuss.elastic.co/t/how-to-query-dataoptionatime-field/154437>\
**Category:** Elasticsearch\
**Created:** [October 29, 2018, 12:25pm UTC](https://discuss.elastic.co/t/how-to-query-dataoptionatime-field/154437 "2018-10-29T12:25:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vladimir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vladimir/32/91042_2.png) [@Vladimir](https://discuss.elastic.co/u/Vladimir)\
**Post date:** [October 29, 2018, 12:25pm UTC](https://discuss.elastic.co/t/how-to-query-dataoptionatime-field/154437/1 "2018-10-29T12:25:55Z")

</div>

Can somebody explain me how to format date for Elastic query if I want to get events which are **gt** than **YYYY-MM-DD\T00:00:00**? The ES date mapping format is **dateOptionalTime**. ES version is 2. I am doing it in PHP script which generates this query:

```auto
{
    "sort": {
        "publishDate": "desc"
    },
    "query": {
        "bool": {
            "filter": [
                {
                    "range": {
                        "publishDate": {
                            "lte": "2018-10-29",
                            "gt": "2018-10-22T00:00:00"
                        }
                    }
                },
                {
                    "terms": {
                        "ico": [
                            31322832,
                            31400396
                        ]
                    }
                }
            ]
        }
    }
}

```

The first date **2018-10-29** works fine but the second **2018-10-22T00:00:00** throws an error: **Invalid format: "2018-10-22T00:00:00" is malformed at "T00:00:00"** I found the format on [this page](https://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateOptionalTimeParser--) and dont know what is wrong with that.

The second question is if this is the way to search dates when I need to check hours and minutes.

---

<div class="post-metadata">

**Author:** ![Vladimir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vladimir/32/91042_2.png) [@Vladimir](https://discuss.elastic.co/u/Vladimir)\
**Post date:** [October 29, 2018, 12:43pm UTC](https://discuss.elastic.co/t/how-to-query-dataoptionatime-field/154437/2 "2018-10-29T12:43:27Z")

</div>

Teh problem was the request leads to two indexes and I checked only one of them. The second one has mapping like `"publishDate": {"type": "date", "format": "date"}`

---

<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 26, 2018, 12:56pm UTC](https://discuss.elastic.co/t/how-to-query-dataoptionatime-field/154437/3 "2018-11-26T12:56:35Z")

</div>

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