# Ruby set first day of next month?

**URL:** <https://discuss.elastic.co/t/ruby-set-first-day-of-next-month/189654>\
**Category:** Logstash\
**Created:** [July 10, 2019, 4:40am UTC](https://discuss.elastic.co/t/ruby-set-first-day-of-next-month/189654 "2019-07-10T04:40:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sjaak01](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Sjaak01](https://discuss.elastic.co/u/Sjaak01)\
**Post date:** [July 10, 2019, 4:40am UTC](https://discuss.elastic.co/t/ruby-set-first-day-of-next-month/189654/1 "2019-07-10T04:40:43Z")

</div>

Hi,

Is it possible to get the first day of the next month using ruby? I need to to update some documents with a future date in a format such as 2019/07/01.

I tried using date and today but none of them seem to work. Logstash either crashes on start or gives error such as `Ruby exception occurred: uninitialized constant LogStash::Filters::Ruby::Today`.

```
ruby {
   code => "event.set('current_time', Today.at_beginning_of_month.next_month)"
}
```

---

<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:** [July 10, 2019, 2:32pm UTC](https://discuss.elastic.co/t/ruby-set-first-day-of-next-month/189654/2 "2019-07-10T14:32:37Z")

</div>

> [@Sjaak01](#):
>
> Is it possible to get the first day of the next month using ruby?

Probably, but you cannot use RoR or active\_support extensions. You will have to do it the hard way. Something like this perhaps?...

```
    ruby {
        code => '
            t = Date.today
            y = t.year
            m = t.month
            if m == 12
                m = 1
                y += 1
            else
                m += 1
            end
            event.set("nextMonth", Date.new(y, m, 1).to_s)
        '
    }

```

I have not tested that for anything except today.

---

<div class="post-metadata">

**Author:** ![Sjaak01](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Sjaak01](https://discuss.elastic.co/u/Sjaak01)\
**Post date:** [July 11, 2019, 11:35pm UTC](https://discuss.elastic.co/t/ruby-set-first-day-of-next-month/189654/3 "2019-07-11T23:35:01Z")

</div>

Thanks, that script works beautifully.

---

<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:** [August 8, 2019, 11:35pm UTC](https://discuss.elastic.co/t/ruby-set-first-day-of-next-month/189654/4 "2019-08-08T23:35:08Z")

</div>

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