# How to save german date text\_field in database

**URL:** <https://www.ruby-forum.com/t/how-to-save-german-date-text-field-in-database/168808>\
**Category:** Rails\
**Created:** [May 26, 2009, 9:48am UTC](https://www.ruby-forum.com/t/how-to-save-german-date-text-field-in-database/168808 "2009-05-26T09:48:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [May 26, 2009, 9:48am UTC](https://www.ruby-forum.com/t/how-to-save-german-date-text-field-in-database/168808/1 "2009-05-26T09:48:06Z")

</div>

Hi Ruby Comunity!

To display a database stored date like %Y-%m-%d into a text\_field with  
german format %d.%m.Y. is not so difficult. For conversion you can do  
something like

\<%= f.text\_field :date\_test, :value =\>  
@tbl\_datum.date\_test.strftime(’%d.%m.%Y’)%\>

or use a helper.

But how can I save it from the form back INTO the database.

I tried it like in Rubycast Nr. 32 “time in textfield” but I think it’s  
not the same because you have to convert the date first from %d.%m.%Y to  
database format%Y-%m-%d.

I tried many (3-days long), but nothings work.

Can somebody help me please, I’m really desperate:-(

Thanks ahead for every proposal!

Regs,

Herman

---

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [May 26, 2009, 10:08am UTC](https://www.ruby-forum.com/t/how-to-save-german-date-text-field-in-database/168808/2 "2009-05-26T10:08:13Z")

</div>

Hallo Hermann,  
Try to convert the text field param to a Date Object using the various  
parsing options provided with the Date class. Then save this to the  
database.  
Greetings, mike

2009/5/26, Herman Müller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> or use a helper.
> 
> Thanks ahead for every proposal!
> 
> Regs,
> 
> ## Herman
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Von meinen Mobilgerät aus gesendet

---

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [May 26, 2009, 10:31am UTC](https://www.ruby-forum.com/t/how-to-save-german-date-text-field-in-database/168808/3 "2009-05-26T10:31:41Z")

</div>

Hi Mike,

thanks for your rapid answer.

Can you give me an example how to do this?

Thanks in front!

Regs

Herman

mike wrote:

> Hallo Hermann,  
> Try to convert the text field param to a Date Object using the various  
> parsing options provided with the Date class. Then save this to the  
> database.  
> Greetings, mike

---

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [May 26, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/how-to-save-german-date-text-field-in-database/168808/4 "2009-05-26T12:23:38Z")

</div>

Herman MÃ¼ller wrote:

> Hi Ruby Comunity!
> 
> To display a database stored date like %Y-%m-%d into a text\_field with  
> german format %d.%m.Y. is not so difficult. For conversion you can do  
> something like
> 
> \<%= f.text\_field :date\_test, :value =\>  
> @tbl\_datum.date\_test.strftime(’%d.%m.%Y’)%\>
> 
> or use a helper.
> 
> But how can I save it from the form back INTO the database.
> 
> I tried it like in Rubycast Nr. 32 “time in textfield” but I think it’s  
> not the same because you have to convert the date first from %d.%m.%Y to  
> database format%Y-%m-%d.
> 
> I tried many (3-days long), but nothings work.
> 
> Can somebody help me please, I’m really desperate:-(
> 
> Thanks ahead for every proposal!
> 
> Regs,
> 
> Herman

This is how I do it. Unless until I digg into i18n

Put into application.rb

def update\_date(\*args)  
rec = args.shift  
args.each do |e|  
d,m,y,time = params[rec][e].split(/.| /)  
params[rec][e] = y + ‘-’ + m + ‘-’ + d + ’ ’ + time unless y.nil?  
end  
end

Then call:  
update\_date(:doc, :time\_created, :time\_closed)

before you update attributes in your controller.

by  
TheR

---

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [May 27, 2009, 11:07am UTC](https://www.ruby-forum.com/t/how-to-save-german-date-text-field-in-database/168808/5 "2009-05-27T11:07:37Z")

</div>

Herman MÃ¼ller wrote:

> Hi Damjan,  
> Hi Mike,
> 
> I made a helper according to the Railscast Nr. 32 “time in textfield”  
> and played on Mikes advice a little bit with the Date Object and hurray  
> it works.
> 
> def date\_test\_string  
> date\_test.strftime(’%d.%m.%Y’)  
> end
> 
> def date\_test\_string=(date\_test\_str)  
> self.date\_test = Date.strptime(date\_test\_str, ‘%d.%m.%Y’)  
> rescue ArgumentError  
> @date\_test\_invalid = true
> 
> end
> 
> def validate  
> errors.add(:date\_test, “is invalid”) if @date\_test\_invalid  
> end
> 
> @Damjan:
> 
> The example you wrote, what you mean with:
> 
> update\_date(:doc, :time\_created, :time\_closed)etc.
> 
> are that the date-attributes inside the model
> 
> like \<%= f.text\_field :date\_test %\> ?
> 
> Regs
> 
> Herman

Yes. I prefere:  
\<%= text\_field :doc, :date\_test %\>  
or in my case I use calendar\_date\_select  
\<%= calendar\_date\_select :doc, :time\_closed, :time =\> true, :size =\> 15  
%\>

update\_date method will update params[:doc][:time\_created] and  
params[:doc][:time\_closed] fields.

You call it before updating fields in a model with values obtained from  
browser.

def save  
@doc = Doc.find(params[:id])  
update\_date(:doc, :time\_created, :time\_closed)  
if @doc.update\_attributes(params[:doc])  
…  
…  
end

by  
TheR

---

<div class="post-metadata">

**Author:** ![pink-ink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pink-ink](https://www.ruby-forum.com/u/pink-ink)\
**Post date:** [May 26, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/how-to-save-german-date-text-field-in-database/168808/6 "2009-05-26T14:04:06Z")

</div>

Hi Damjan,  
Hi Mike,

I made a helper according to the Railscast Nr. 32 “time in textfield”  
and played on Mikes advice a little bit with the Date Object and hurray  
it works.

def date\_test\_string  
date\_test.strftime(’%d.%m.%Y’)  
end

def date\_test\_string=(date\_test\_str)  
self.date\_test = Date.strptime(date\_test\_str, ‘%d.%m.%Y’)  
rescue ArgumentError  
@date\_test\_invalid = true

end

def validate  
errors.add(:date\_test, “is invalid”) if @date\_test\_invalid  
end

@Damjan:

The example you wrote, what you mean with:

update\_date(:doc, :time\_created, :time\_closed)etc.

are that the date-attributes inside the model

like \<%= f.text\_field :date\_test %\> ?

Regs

Herman
