# How to add Date in mutate add field

**URL:** https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601
**Category:** Logstash
**Created:** [October 9, 2020, 5:06pm UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601 "2020-10-09T17:06:18Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![tusharnemade](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@tusharnemade](https://discuss.elastic.co/u/tusharnemade)
#### Post date: [October 9, 2020, 5:06pm UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/1 "2020-10-09T17:06:19Z")

</div>

Hi Team

I am using logstash v7.8 to extract data from ES v7.8 index and inserting respective records to ES v7.8 index-2.

While doing so , I want to use filter --\> mutate --\> add\_field ==\> { "doc\_date" =\> "now-1d" }

How can i achieve "now-1d" with particular date format and timezone in logstash.

Could you please help me ..

Thanks  
Tushar Nemade

---

<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: [October 9, 2020, 5:58pm UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/2 "2020-10-09T17:58:56Z")

</div>

```
ruby { code => 'event.set("[date]", Time.at(Time.now.to_i - 86400))' }

```

will produce

```
      "date" => 2020-10-08T17:55:05.000Z,
"@timestamp" => 2020-10-09T17:55:05.398Z,

```

You can use strftime if that is not the format you want.

---

<div class="post-metadata">

### Author: ![tusharnemade](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@tusharnemade](https://discuss.elastic.co/u/tusharnemade)
#### Post date: [October 10, 2020, 5:02am UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/3 "2020-10-10T05:02:41Z")

</div>

Hello

Thanks Badger for your response.

Could you please help me to understand , how to call this ruby or srftime code in add\_filed of mutate filter ..

Thanks  
Tushar Nemade

---

<div class="post-metadata">

### Author: ![tusharnemade](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@tusharnemade](https://discuss.elastic.co/u/tusharnemade)
#### Post date: [October 10, 2020, 5:11am UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/4 "2020-10-10T05:11:07Z")

</div>

Hello

I understand , below one will be my code with time format. But I am unable to use this in add\_filed .. could someone please help me with that ..

`ruby { code => "event.set('[date]', Time.at(Time.now.to_i - 86400)).time.strftime('%Y-%m-%d'))" }`

Thanks  
Tushar Nemade

---

<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: [October 10, 2020, 1:46pm UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/5 "2020-10-10T13:46:11Z")

</div>

You do not use add\_field or mutate, you use a ruby filter.

---

<div class="post-metadata">

### Author: ![tusharnemade](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@tusharnemade](https://discuss.elastic.co/u/tusharnemade)
#### Post date: [October 10, 2020, 2:10pm UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/6 "2020-10-10T14:10:43Z")

</div>

Hello

Thanks.

When i am using  
`code => 'event.set("[curr_date]", Time.at(Time.now.to_i - 86400).time.strftime("%Y-%m-%d"))'`

I am getting below error :

`[2020-10-10T19:36:02,407][ERROR][logstash.filters.ruby][main][96fd5f758cc12f0b36825b45c2bde51e89f33542b4a4727e21bf3df9408bfdc4] Ruby exception occurred: undefined method `time' for 2020-10-09 19:36:02 +0530:Time /mnt/logstash-7.8.0/vendor/bundle/jruby/2.5.0/gems/awesome_print-1.7.0/lib/awesome_print/formatters/base_formatter.rb:31: warning: constant ::Fixnum is deprecated { "host" => "jes-ext-pd-es-fldapplogs-02", "tags" => [[0] "_rubyexception" ], "@version" => "1", "@timestamp" => 2020-10-10T14:06:02.288Z, "message" => "" }`

Could you please help me to fix this error... I am trying to fix this .. but somewhere syntax error i m doing that is not being captured by me yet ...

Thanks  
Tushar Nemade

---

<div class="post-metadata">

### Author: ![tusharnemade](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@tusharnemade](https://discuss.elastic.co/u/tusharnemade)
#### Post date: [October 10, 2020, 2:17pm UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/7 "2020-10-10T14:17:07Z")

</div>

Finally its working ...

`code => 'event.set("[curr_date]", Time.at(Time.now.to_i - 86400).strftime("%Y-%m-%d"))'`

Now I want to add timezone ...

Will update in here

---

<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: [October 10, 2020, 2:21pm UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/8 "2020-10-10T14:21:22Z")

</div>

> [@tusharnemade](#):
>
> Time.at(Time.now.to\_i - 86400).time.strftime("%Y-%m-%d")

I think that should be

```
Time.at(Time.now.to_i - 86400).strftime("%Y-%m-%d")

```

---

<div class="post-metadata">

### Author: ![tusharnemade](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@tusharnemade](https://discuss.elastic.co/u/tusharnemade)
#### Post date: [October 10, 2020, 2:24pm UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/9 "2020-10-10T14:24:49Z")

</div>

Hello

````
```filter {
ruby { 
code => 'event.set("[curr_date]", Time.at(Time.now.to_i - 86400).localtime("+05:30").strftime("%Y-%m-%d %H:%M:%S"))'
}
}``` 

````

Output --\>

`"curr_date" => "2020-10-09 19:49:17"`

This is as expected.

Thanks Badger , for your help and support which made this working ...

Much appreciated...

Thanks  
Tushar Nemade

---

<div class="post-metadata">

### Author: ![tusharnemade](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@tusharnemade](https://discuss.elastic.co/u/tusharnemade)
#### Post date: [October 14, 2020, 11:15am UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/10 "2020-10-14T11:15:05Z")

</div>

Hello Team

From Above i got how to get the curr\_date value of time , but how to add this curr\_date as field in elasticsearch index -- output one.

could you please help me.

Thanks  
Tushar Nemade

---

<div class="post-metadata">

### Author: ![tusharnemade](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@tusharnemade](https://discuss.elastic.co/u/tusharnemade)
#### Post date: [October 15, 2020, 5:46am UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/11 "2020-10-15T05:46:21Z")

</div>

can someone please help me

---

<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: [November 12, 2020, 5:46am UTC](https://discuss.elastic.co/t/how-to-add-date-in-mutate-add-field/251601/12 "2020-11-12T05:46:29Z")

</div>

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