# Ruby filter to convert date

**URL:** https://discuss.elastic.co/t/ruby-filter-to-convert-date/193295
**Category:** Logstash
**Created:** [August 1, 2019, 10:24am UTC](https://discuss.elastic.co/t/ruby-filter-to-convert-date/193295 "2019-08-01T10:24:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![HFolly](https://avatars.discourse-cdn.com/v4/letter/h/ac8455/32.png) [@HFolly](https://discuss.elastic.co/u/HFolly)
#### Post date: [August 1, 2019, 10:24am UTC](https://discuss.elastic.co/t/ruby-filter-to-convert-date/193295/1 "2019-08-01T10:24:29Z")

</div>

Hi there.

In my Logstash configuration I need a Ruby filter to convert date from a given collection of strings.

My collection uses an structure with 3 values containing a field name, it's value and it's data type:

```
"Colecs": [
		{
			"Value": {
				"Value": "Tester"
			},
			"FieldName": {
				"Value": "Name"
			},
			"Type": {
				"Value": "String"
			}
		},
		{
			"Value": {
				"Value": "7/17/2019 3:58:31 PM"
			},
			"FieldName": {
				"Value": "DateOfBirth"
			},
			"Type": {
				"Value": "DateTime"
			}
		}
	],

```

This is my Ruby filter:

```
ruby{
	code => "

		require 'date'
		require 'time'

		event.get('[Colecs]').each do |item|

			varValue = item['Value']['Value']
			varFieldName = item['FieldName']['Value']
			varType = item['Type']['Value']

			if varType == 'Integer'
				event.set( varFieldName, varValue.to_i)
			elsif varType == 'Float'			
				event.set( varFieldName, varValue.to_f)

			elsif varType == 'DateTime'

				event.set( varFieldName, { 'date' => varValue} ) <--- This is where I convert dates

			else
				event.set( varFieldName, varValue.to_s)
			end
		end
	"
}

```

Leaving the code as this will produce me the date as string. I could use the Logstash date filter to convert it to date but I don't know how I can do it because after running the Ruby filter what I get is a field with the FieldName, but it must be generic, I'll never know it's name.  
Just to clarify, the above code will produce a field in the root of my JSON as:

```
"DateOfBirth": {
		"date": "2019-07-17T15:58:31.000Z"
	},

```

I don't know the "DateOfBirth" name to use it on date filter.

In the Ruby filter, I have tried to convert date as the following:

* * *

**.to\_datetime**

```
event.set( varFieldName, { 'date' => varValue.to_datetime} )

```

Result: I get this following error on Logstash log

```
[ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `to_datetime' for "7/17/2019 3:58:31 PM":String

```

* * *

**Using time conversion**

```
varDateStringToDate = Time.strptime(varValue, '%m/%d/%Y %H:%M:%S %P')
event.set( varFieldName, { 'date' => varDateStringToDate.to_datetime} )

```

Result: I get this following error on Logstash log

```
[ERROR][logstash.filters.ruby] Ruby exception occurred: Missing Converter handling for full class name=org.jruby.RubyObjectVar3, simple name=RubyObjectVar3

```

* * *

Can someone help me please?

---

<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: [August 1, 2019, 12:28pm UTC](https://discuss.elastic.co/t/ruby-filter-to-convert-date/193295/2 "2019-08-01T12:28:48Z")

</div>

> [@HFolly](#):
>
> varDateStringToDate = Time.strptime(varValue, '%m/%d/%Y %H:%M:%S %P') event.set( varFieldName, { 'date' =\> varDateStringToDate.to\_datetime} )

I do not understand what you are trying to do with .to\_datetime. If you want it to be a LogStash::Timestamp you can just

```
event.set( varFieldName, { 'date' => varDateStringToDate} )

```

---

<div class="post-metadata">

### Author: ![HFolly](https://avatars.discourse-cdn.com/v4/letter/h/ac8455/32.png) [@HFolly](https://discuss.elastic.co/u/HFolly)
#### Post date: [August 1, 2019, 12:38pm UTC](https://discuss.elastic.co/t/ruby-filter-to-convert-date/193295/3 "2019-08-01T12:38:01Z")

</div>

Thank you @Badger , it was just that simple.  
Now it's working 🙂

---

<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: [August 29, 2019, 12:38pm UTC](https://discuss.elastic.co/t/ruby-filter-to-convert-date/193295/4 "2019-08-29T12:38:08Z")

</div>

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