How to calculate old date from today by subtracting given years, months, days in logstash?

I want to subtract current date with given years, months, days using logstash pipeline.
For example : Today = 1 Aug 2021
Given values : Years=25, Months=10, Days=10
Expected date : 22 Sept 1995

My logstash filter section :

ruby {
        code => '
            years = 25
            months = 10
            days = 10
            datenow = Time.now.utc.to_date
            delUntilYear = datenow.year - "#{years}".to_i
            delUntilMonth = datenow.month - "#{months}".to_i
            delUntilDay = datenow.day - "#{days}".to_i
            event.set("[deleteTillDate]", "#{delUntilYear}-#{delUntilMonth}-#{delUntilDay} 00:00:00.000")
        '
    }

This is giving negative values for delUntilMonth and delUntilDay . How to handle such situation ?

I answered how to do that using DateTime methods like prev_month on SO here.

Yes, Perfect solution. Thanks @Badger :slight_smile:

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