# How to convert extract month from date

**URL:** https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035
**Category:** Logstash
**Created:** [April 13, 2021, 3:59pm UTC](https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035 "2021-04-13T15:59:48Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)
#### Post date: [April 13, 2021, 3:59pm UTC](https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035/1 "2021-04-13T15:59:48Z")

</div>

Hi,  
I have below date format .  
First\_date : Feb-21 (MMM-yy)  
Second\_date: 12/02/2021 (MM/dd/yyyy)

first, I want new field like below:  
from first date I need month . so output should be  
first\_date\_month : 02  
second date month: 12

Second, I want it to convert as follow:  
if first\_date\_month is in between 01 to 03 then create new field as below  
quarter :Q1

I know this is bit confusing. Please help me with this.

Thanks in advance

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 13, 2021, 4:20pm UTC](https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035/2 "2021-04-13T16:20:01Z")

</div>

You can extract the month fields using either dissect or grok. You can map an abbreviation of a month into a number using a translate filter. You can also use translate to map months to quarters.

---

<div class="post-metadata">

### Author: ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)
#### Post date: [April 15, 2021, 7:50am UTC](https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035/3 "2021-04-15T07:50:17Z")

</div>

I have tried this  
if[start\_date]  
{  
date {  
match =\> ["start\_date" , "MMM-yy","yy-MMM"]  
target =\> "startdatetarget"  
}  
}

```
grok {match => ["startdatetarget", "%{YEAR:year}-%{MONTH:month}"]
add_field => ["as_of_date", "%{month}"]}	

```

date { match =\> ["actual\_closed\_month" , "MM/dd/yyyy","MM/dd/yy","MM-dd-yy"]  
target =\> "actualclosedmonthtarget"  
}  
ruby {  
code =\> "event.set('[start\_date\_epoch]', (1000_event.get('startdatetarget').to\_f).round(0))  
event.set('[actual\_closed\_month\_epoch]', (1000_event.get('actualclosedmonthtarget').to\_f).round(0))"  
}

```
	======================================================================
	output I am getting is 
	First scenario--> example 
	"start_date" : "Nov-19",
	"startdatetarget" : " **2019** -10-31T18:30:00.000Z",
	Nov 19 is 2019-10 (Oct)
	as_of_date I am not getting any value for this 
	Second Scenario -->
	"_source" : {
      "actualclosedmonthtarget" : " **0019** -05-01T18:06:32.000Z",
      "actual_closed_month" : "5/2/19",
      "actual_closed_month_epoch" : -61557170008000
    }

```

actual closed month value is 5/2/19 which is MM/DD/yy but target value is coming as 0019 instead of 2019.

Please help me with this .

Thanks in advance

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 15, 2021, 4:14pm UTC](https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035/4 "2021-04-15T16:14:31Z")

</div>

> [@Pallavibhushan](#):
>
> match =\> ["actual\_closed\_month" , "MM/dd/yyyy","MM/dd/yy","MM-dd-yy"]

The Java [documentation](https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html) says "For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D."

The patterns in a date filter are tried in order. In this case the first one will work, so the second one is never tried. I would suggest (but have not tested) changing the order of entries.

```
match => ["actual_closed_month", "MM/dd/yy", "MM-dd-yy", "MM/dd/yyyy"]

```

---

<div class="post-metadata">

### Author: ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)
#### Post date: [April 16, 2021, 9:35am UTC](https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035/5 "2021-04-16T09:35:42Z")

</div>

Thanks for reply it worked.  
But still I am not getting how to extract month.  
I tried below code  
date { match =\> ["actual\_closed\_month" , "MM/dd/yy","MM-dd-yy","MM/dd/yyyy"]  
target =\> "actualclosedmonthtarget"}  
grok { match =\> ["actualclosedmonthtarget", "^%{YEAR}-%{MONTH:actual\_month}"]}

I am not getting proper response  
"actual\_epoch" : 1604255400000,  
"actualclosedmonthtarget" : "2020-11-01T18:30:00.000Z",  
"actual\_month" : "1",  
"actual\_closed\_month" : "11/2/20"  
November month is coming as 1.

Am I doing something wrong with the code

Thanks in advance

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 16, 2021, 2:35pm UTC](https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035/6 "2021-04-16T14:35:16Z")

</div>

> [@Pallavibhushan](#):
>
> {MONTH:actual\_month}

I am surprised that matches at all, since MONTH matches the name or abbreviation of a name. You could try MONTHNUM. If that still matches 1 then try `-%{MONTHNUM:actual\_month}-"

---

<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: [May 14, 2021, 2:36pm UTC](https://discuss.elastic.co/t/how-to-convert-extract-month-from-date/270035/7 "2021-05-14T14:36:00Z")

</div>

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