# Hour\_minute date format in range queries

**URL:** https://discuss.elastic.co/t/hour-minute-date-format-in-range-queries/66299
**Category:** Elasticsearch
**Created:** [November 16, 2016, 8:11pm UTC](https://discuss.elastic.co/t/hour-minute-date-format-in-range-queries/66299 "2016-11-16T20:11:24Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Adam\_M](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Adam\_M](https://discuss.elastic.co/u/Adam_M)
#### Post date: [November 16, 2016, 8:11pm UTC](https://discuss.elastic.co/t/hour-minute-date-format-in-range-queries/66299/1 "2016-11-16T20:11:24Z")

</div>

Hello,

I'm preparing bus timetable and I got a problem with query.  
My mapping (below) has `hour` field in `hour_minute` format.

```json
{
  "stops": {
    "properties": {
      "dayType": {
        "type": "string"
      },
      "stopNumber": {
        "type": "string"
      },
      "hour": {
        "format": "hour_minute",
        "type": "date"
      },
      "stopDetails": {
        "type": "string"
      },
      "location": {
        "type": "geo_point"
      },
      "lineNumber": {
        "type": "string"
      }
    }
  }
}

```

I would like to have the times starting from `now` and then sort them ascending but querying from now doesn't work. I know it's a problem with timestamp that stays behind `now` so I started passing time value like `22:34` and it works but returns documents only to 23:59.

I would really love my query to return documents, say from 22:34 to 2:15 but I don't know how to do it.

```json
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "range": {
                      "hour": {
                        "gte": "now",
                        "format": "HH:mm",
                        "include_lower": true,
                        "include_upper": true
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)
#### Post date: [November 17, 2016, 8:55am UTC](https://discuss.elastic.co/t/hour-minute-date-format-in-range-queries/66299/2 "2016-11-17T08:55:06Z")

</div>

Hi,

I think the problem you are has to do with the ways dates are stored in Elasticsearch internally and the way "now" is resolved. To start with the first, dates are converted to UTC and stored as a long number representing milliseconds-since-the-epoch. So if you index a date like "03:00" it will be stored as the long value "10800000" internally.

On the other hand, "now" gets resolved to the current UTC time (the format doesn't really affect that, it is just enabling parsing the date string), so it will be much larger than the dates you are storing. If you want to store only the hours/minutes of your bus depatures, you need to somehow subtract the time when "today" starts from that value. I tried some date math like `"now-now/d"` which I thought would do just that (subtract "now" rounded to the current day from "now"), but that doesn't seem to work. But something along those lines. If that doesn't work you might need to try to figure out the milliseconds-since-start-of-day somehow in your application.

---

<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: [December 15, 2016, 8:55am UTC](https://discuss.elastic.co/t/hour-minute-date-format-in-range-queries/66299/3 "2016-12-15T08:55:07Z")

</div>

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