# Classifying records into quarters based on timestamps

**URL:** <https://discuss.elastic.co/t/classifying-records-into-quarters-based-on-timestamps/187138>\
**Category:** Logstash\
**Created:** [June 24, 2019, 1:14pm UTC](https://discuss.elastic.co/t/classifying-records-into-quarters-based-on-timestamps/187138 "2019-06-24T13:14:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michiele\_Ogbagabir](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Michiele\_Ogbagabir](https://discuss.elastic.co/u/Michiele_Ogbagabir)\
**Post date:** [June 24, 2019, 1:14pm UTC](https://discuss.elastic.co/t/classifying-records-into-quarters-based-on-timestamps/187138/1 "2019-06-24T13:14:48Z")

</div>

I have records that I would like to classify into quarters based on the timestamp value.  
The logic I am shooting for is:

If the month of the timestamp is 1, 2, or 3 then add\_field { "Quarter" =\> "Q1"}  
If the month of the timestamp is 4,5 or 6 then add\_field { "Quarter" =\> "Q2"}  
and so on.

How do I compare only the month value of my timestamp to make this if-else clause?

Thanks

---

<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:** [June 24, 2019, 1:53pm UTC](https://discuss.elastic.co/t/classifying-records-into-quarters-based-on-timestamps/187138/2 "2019-06-24T13:53:58Z")

</div>

The following would work. Not convinced it is the best way though 🙂

```
    ruby {
        code => '
            epochMS = (event.get("@timestamp").to_f * 1000).to_s
            monthNum = DateTime.strptime(epochMS, "%Q").month
            quarter = (monthNum.to_f / 3).ceil
            event.set("Quarter", "Q#{quarter}")
        '
    }
```

---

<div class="post-metadata">

**Author:** ![Michiele\_Ogbagabir](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Michiele\_Ogbagabir](https://discuss.elastic.co/u/Michiele_Ogbagabir)\
**Post date:** [June 24, 2019, 6:00pm UTC](https://discuss.elastic.co/t/classifying-records-into-quarters-based-on-timestamps/187138/3 "2019-06-24T18:00:51Z")

</div>

Thanks Badger!

---

<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 22, 2019, 6:00pm UTC](https://discuss.elastic.co/t/classifying-records-into-quarters-based-on-timestamps/187138/4 "2019-07-22T18:00:52Z")

</div>

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