RE: Date transform


#1

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.


#2

Or have a look at my validates_date_time plugin

http://svn.viney.net.nz/things/rails/plugins/validates_date_time

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

-Jonathan.


#3

Thks Dan and jonathan, i look your solution.


#4

“irong” == irong 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
http://www.livejournal.com/users/cdybedahl/
“Facts are for people with weak opinions.” – Lars Willför, I]M


#5

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

-Jonathan.


#6

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


#7

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.