# Filtering a date field by month

**URL:** https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792
**Category:** Elasticsearch
**Created:** [February 23, 2012, 2:56pm UTC](https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792 "2012-02-23T14:56:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Paco\_Guzman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paco_guzman/32/2460_2.png) [@Paco\_Guzman](https://discuss.elastic.co/u/Paco_Guzman)
#### Post date: [February 23, 2012, 2:56pm UTC](https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792/1 "2012-02-23T14:56:12Z")

</div>

Hi everyone,

I'm not find a way to retrieve all documents that have a release date  
(a field indexed like a date) where its month is one specific.

I'm only find range queries or gt, lt, filters

Thanks in advance

---

<div class="post-metadata">

### Author: ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)
#### Post date: [February 23, 2012, 6:53pm UTC](https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792/2 "2012-02-23T18:53:43Z")

</div>

On Feb 23, 6:56 am, Paco Guzmán [pacoguzm...@gmail.com](mailto:pacoguzm...@gmail.com) wrote:

> I'm not find a way to retrieve all documents that have a release date  
> (a field indexed like a date) where its month is one specific.
> 
> I'm only find range queries or gt, lt, filters

Being able to query directly using year/month/week/day/hour/minute  
intervals would indeed be useful. But you can build a range query like  
so (using the Joda-Time API):

buildQuery("timestamp", YearMonth.parse("2012-02"),  
DateTimeZone.UTC);

public QueryBuilder buildQuery(String field, YearMonth month,  
DateTimeZone tz) {  
Interval interval = forMonth(month, tz);  
return QueryBuilders.rangeQuery(field)  
.gte(interval.getStart().toString()) // assuming  
ISODateTimeFormat  
.lt(interval.getEnd().toString());  
}

private Interval forMonth(YearMonth month, DateTimeZone tz) {  
return new Interval(toDateTime(month, tz),  
toDateTime(month.plusMonths(1), tz));  
}

private DateTime toDateTime(YearMonth month, DateTimeZone tz) {  
return new DateTime(month.getYear(), month.getMonthOfYear(), 1, 0,  
0, tz);  
}

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [February 26, 2012, 10:00am UTC](https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792/3 "2012-02-26T10:00:55Z")

</div>

Do you mean give me all documents where the month is "June", regardless of the year?

On Thursday, February 23, 2012 at 4:56 PM, Paco Guzmán wrote:

> Hi everyone,
> 
> I'm not find a way to retrieve all documents that have a release date  
> (a field indexed like a date) where its month is one specific.
> 
> I'm only find range queries or gt, lt, filters
> 
> Thanks in advance

---

<div class="post-metadata">

### Author: ![mrugankparikh](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mrugankparikh](https://discuss.elastic.co/u/mrugankparikh)
#### Post date: [April 7, 2014, 10:22am UTC](https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792/4 "2014-04-07T10:22:36Z")

</div>

Yes, we would like to filter all rows where month is June . We have a single column with date field type having 10 years of data

---

<div class="post-metadata">

### Author: ![Benjamin\_Hodges](https://avatars.discourse-cdn.com/v4/letter/b/90db22/32.png) [@Benjamin\_Hodges](https://discuss.elastic.co/u/Benjamin_Hodges)
#### Post date: [August 19, 2016, 2:13pm UTC](https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792/5 "2016-08-19T14:13:41Z")

</div>

Was this ever resolved? I am having this exact issue.

---

<div class="post-metadata">

### Author: ![prabhath](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@prabhath](https://discuss.elastic.co/u/prabhath)
#### Post date: [April 4, 2017, 10:45pm UTC](https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792/6 "2017-04-04T22:45:18Z")

</div>

Do we solution for this.

---

<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:01pm UTC](https://discuss.elastic.co/t/filtering-a-date-field-by-month/6792/7 "2017-07-05T22:01:29Z")

</div>


