Ruby set first day of next month?

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)"
}

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.

1 Like

Thanks, that script works beautifully.

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