# DateTime Function in Ruby logstash filter

**URL:** <https://discuss.elastic.co/t/datetime-function-in-ruby-logstash-filter/78126>\
**Category:** Logstash\
**Created:** [March 10, 2017, 10:13am UTC](https://discuss.elastic.co/t/datetime-function-in-ruby-logstash-filter/78126 "2017-03-10T10:13:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurveer\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@Gurveer\_Singh](https://discuss.elastic.co/u/Gurveer_Singh)\
**Post date:** [March 10, 2017, 10:13am UTC](https://discuss.elastic.co/t/datetime-function-in-ruby-logstash-filter/78126/1 "2017-03-10T10:13:05Z")

</div>

Hi,

I am trying to add a new field which will have value, **week of year**. Following code is working fine.

ruby {  
code =\> '  
require "date"  
d = DateTime.new(2017,02,02)  
event["Week\_of\_Year"] = d.cweek  
' }

But If I use following code to make it dynamic, it's not working.  
ruby {  
code =\> '  
require "date"  
d = DateTime.new(event[" **CreateDate**"])  
event["Week\_of\_Year"] = d.cweek  
' }

I am using Logstash version: 2.3.2

Thanks

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [March 13, 2017, 8:42am UTC](https://discuss.elastic.co/t/datetime-function-in-ruby-logstash-filter/78126/2 "2017-03-13T08:42:49Z")

</div>

Does the `CreateDate` field contain a string? If yes, use DateTime.strptime to parse the string into a DateTime object.

---

<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:** [April 10, 2017, 8:42am UTC](https://discuss.elastic.co/t/datetime-function-in-ruby-logstash-filter/78126/3 "2017-04-10T08:42:57Z")

</div>

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