# RE: Date transform

**URL:** <https://www.ruby-forum.com/t/re-date-transform/94877>\
**Category:** Rails\
**Created:** [May 17, 2006, 12:32am UTC](https://www.ruby-forum.com/t/re-date-transform/94877 "2006-05-17T00:32:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 17, 2006, 12:32am UTC](https://www.ruby-forum.com/t/re-date-transform/94877/1 "2006-05-17T00:32:03Z")

</div>

> i want my users to enter a date in “ddmmyyyy” format, do someone know  
> how may i transform it in “yyyy-mm-dd” in the controler  
> before i add it  
> to the base please?

See Time#strftime.

Regards,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 17, 2006, 2:59am UTC](https://www.ruby-forum.com/t/re-date-transform/94877/2 "2006-05-17T02:59:08Z")

</div>

Or have a look at my validates\_date\_time plugin

[http://svn.viney.net.nz/things/rails/plugins/validates\_date\_time](http://svn.viney.net.nz/things/rails/plugins/validates_date_time)

You’d need to add an extra regex to match ddmmyyy.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 17, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/re-date-transform/94877/3 "2006-05-17T12:13:31Z")

</div>

Thks Dan and jonathan, i look your solution.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 17, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/re-date-transform/94877/4 "2006-05-17T12:47:37Z")

</div>

> > > > > “irong” == irong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> In fact, i just want to transform the format : ‘ddmmyyyy’ in  
> ‘yyyy-mm-dd’ in php i create a fonction :

> Is there a command in ruby to do such things please?

irb(main):001:0\> str = “ddmmyyyy”  
=\> “ddmmyyyy”  
irb(main):002:0\> str.sub(/(…)(…)(…)/) {“#{$3}-#{$2}-#{$1}”}  
=\> “yyyy-mm-dd”  
irb(main):003:0\>

–  
Calle D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.livejournal.com/users/cdybedahl/](http://www.livejournal.com/users/cdybedahl/)  
“Facts are for people with weak opinions.” – Lars Willför, I]M

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 17, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/re-date-transform/94877/5 "2006-05-17T14:39:19Z")

</div>

string = ‘18052006’  
Date.new(\*string.scan(/(\d{2})(\d{2})(\d{4})/).flatten.reverse.map(&:to\_i))

-Jonathan.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 17, 2006, 12:33pm UTC](https://www.ruby-forum.com/t/re-date-transform/94877/6 "2006-05-17T12:33:58Z")

</div>

In fact, i just want to transform the format : ‘ddmmyyyy’ in  
‘yyyy-mm-dd’ in php i create a fonction :

$variable = “12052006” ;

function datesql($variable)  
{  
$d1 = substr($variable, 4, 4);  
$d2 = substr($variable, 2, 2);  
$d3 = substr($variable, 0, 2);  
$variable = “$d1-$d2-$d3”;  
return $variable;  
}

it then return :

$variable = “2006-05-12” ;

Is there a command in ruby to do such things please?  
thks for your help

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 18, 2006, 1:17am UTC](https://www.ruby-forum.com/t/re-date-transform/94877/7 "2006-05-18T01:17:30Z")

</div>

Jonathan V. wrote:

> string = ‘18052006’  
> Date.new(\*string.scan(/(\d{2})(\d{2})(\d{4})/).flatten.reverse.map(&:to\_i))
> 
> -Jonathan.

The situation is when i create a person, in the form the user enter his  
date of birth in “ddmmyyyy” format and name, adress etc… Then i call  
in my controler an action called “create\_stagiaire”

which is :

def create\_stagiaire  
@stagiaire = Stagiaire.new(params[:stagiaire])  
if @stagiaire.save  
flash[:notice] = ‘Stagiaire was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

By using ur code is the final code supposed to be something like that? (  
the column name for the date of birth is : dob )

def create\_person

```
string = params[:dob]
Ddn.new(*string.scan(/(\d{2})(\d{2})(\d{4})/).flatten.reverse.map(&:to_i))
@stagiaire = Stagiaire.new(params[:stagiaire],ddn)
if @stagiaire.save
  flash[:notice] = 'Stagiaire was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

? sorry me soo noob in ror :(. I bought the book and done several tuto,  
but never done such things.
