hi all, 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? thks irong -- Posted via http://www.ruby-forum.com/.
> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of irong > Sent: Tuesday, May 16, 2006 4:26 PM > To: rails@lists.rubyonrails.org > Subject: [Rails] Date transform > > > hi all, > > 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.
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. On 5/17/06, Berger, Daniel <Daniel.Berger@qwest.com> wrote:> > -----Original Message----- > > From: rails-bounces@lists.rubyonrails.org > > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of irong > > Sent: Tuesday, May 16, 2006 4:26 PM > > To: rails@lists.rubyonrails.org > > Subject: [Rails] Date transform > > > > > > hi all, > > > > 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. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thks Dan and jonathan, i look your solution. -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
>>>>> "irong" == irong <irong@irong2k.com> 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 Dybedahl <calle@cyberpomo.com> http://www.livejournal.com/users/cdybedahl/ "Facts are for people with weak opinions." -- Lars Willf?r, I]M
string = ''18052006'' Date.new(*string.scan(/(\d{2})(\d{2})(\d{4})/).flatten.reverse.map(&:to_i)) -Jonathan. On 5/17/06, Calle Dybedahl <rails@cyberpomo.com> wrote:> >>>>> "irong" == irong <irong@irong2k.com> 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 Dybedahl <calle@cyberpomo.com> > http://www.livejournal.com/users/cdybedahl/ > "Facts are for people with weak opinions." -- Lars Willf?r, I]M > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jonathan Viney 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. -- Posted via http://www.ruby-forum.com/.
Hi Irong, I would recommend using pop menus for both year, month, and day. This would eliminate parsing of a date from a user and make their life easier as well. BTW, you''ll use the date SQL type or if you''re using migrations, you''ll use the date ruby type. Good luck, -Conrad On 5/16/06, irong <irong@irong2k.com> wrote:> > hi all, > > 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? > > thks > > irong > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060518/d1bf494d/attachment.html