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

**URL:** https://discuss.elastic.co/t/how-to-calculate-old-date-from-today-by-subtracting-given-years-months-days-in-logstash/283349
**Category:** Logstash
**Created:** [September 4, 2021, 4:24pm UTC](https://discuss.elastic.co/t/how-to-calculate-old-date-from-today-by-subtracting-given-years-months-days-in-logstash/283349 "2021-09-04T16:24:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![priyaankaa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priyaankaa/32/89441_2.png) [@priyaankaa](https://discuss.elastic.co/u/priyaankaa)
#### Post date: [September 4, 2021, 4:24pm UTC](https://discuss.elastic.co/t/how-to-calculate-old-date-from-today-by-subtracting-given-years-months-days-in-logstash/283349/1 "2021-09-04T16:24:00Z")

</div>

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 :

```auto
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 ?

---

<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: [September 4, 2021, 4:35pm UTC](https://discuss.elastic.co/t/how-to-calculate-old-date-from-today-by-subtracting-given-years-months-days-in-logstash/283349/2 "2021-09-04T16:35:45Z")

</div>

I answered how to do that using DateTime methods like prev\_month on SO [here](https://stackoverflow.com/a/69057160/11792977).

---

<div class="post-metadata">

### Author: ![priyaankaa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priyaankaa/32/89441_2.png) [@priyaankaa](https://discuss.elastic.co/u/priyaankaa)
#### Post date: [September 4, 2021, 5:36pm UTC](https://discuss.elastic.co/t/how-to-calculate-old-date-from-today-by-subtracting-given-years-months-days-in-logstash/283349/3 "2021-09-04T17:36:53Z")

</div>

Yes, Perfect solution. 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: [October 2, 2021, 5:37pm UTC](https://discuss.elastic.co/t/how-to-calculate-old-date-from-today-by-subtracting-given-years-months-days-in-logstash/283349/4 "2021-10-02T17:37:22Z")

</div>

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