# Strftime is not working Logshtash ruby code

**URL:** https://discuss.elastic.co/t/strftime-is-not-working-logshtash-ruby-code/88008
**Category:** Logstash
**Created:** [June 2, 2017, 2:10am UTC](https://discuss.elastic.co/t/strftime-is-not-working-logshtash-ruby-code/88008 "2017-06-02T02:10:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rpuserh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rpuserh/32/63367_2.png) [@rpuserh](https://discuss.elastic.co/u/rpuserh)
#### Post date: [June 2, 2017, 2:10am UTC](https://discuss.elastic.co/t/strftime-is-not-working-logshtash-ruby-code/88008/1 "2017-06-02T02:10:02Z")

</div>

Hi,  
Im running logstash 5.4 and i need new add\_field with sub date string

im trying filter like this

ruby {  
code =\> "event.set('read\_time' , event.get('@timestamp').strftime('%Y-%m'))"  
}

And it is saying '[2017-06-02T02:05:32,867][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `strftime' for 2017-06-02T01:54:11.000Z:LogStash::Timestamp'

What should I do or what im doing wrong  
Please help on this.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)
#### Post date: [June 2, 2017, 10:01am UTC](https://discuss.elastic.co/t/strftime-is-not-working-logshtash-ruby-code/88008/2 "2017-06-02T10:01:54Z")

</div>

From what I gather, _strftime_ needs to operate on a Time instance, and _@timestamp_ is not one of those.  
The code below should work instead

```auto
ruby {
    code => "event.set('read_time' , Time.at(event.get('@timestamp').to_i).strftime('%Y-%m'))"
}
```

---

<div class="post-metadata">

### Author: ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)
#### Post date: [June 2, 2017, 10:43am UTC](https://discuss.elastic.co/t/strftime-is-not-working-logshtash-ruby-code/88008/3 "2017-06-02T10:43:27Z")

</div>

The LogStash::Timestamp class has a `time` method.

Try...

```auto
ruby {
code => "event.set('read_time' , event.get('@timestamp').time.strftime('%Y-%m'))"
}

```

---

<div class="post-metadata">

### Author: ![rpuserh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rpuserh/32/63367_2.png) [@rpuserh](https://discuss.elastic.co/u/rpuserh)
#### Post date: [June 6, 2017, 11:19pm UTC](https://discuss.elastic.co/t/strftime-is-not-working-logshtash-ruby-code/88008/4 "2017-06-06T23:19:35Z")

</div>

Thank you This was great.

---

<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: [July 4, 2017, 11:19pm UTC](https://discuss.elastic.co/t/strftime-is-not-working-logshtash-ruby-code/88008/5 "2017-07-04T23:19:40Z")

</div>

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