Forum: Ruby on Rails RE: Date transform

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Berger, Daniel (Guest)
on 2006-05-17 00:32
(Received via mailing list)
> 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.
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-05-17 02:59
(Received via mailing list)
Or have a look at my validates_date_time plugin

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

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

-Jonathan.
96e719b743c8cd7bd40ee8dbf936d86e?d=identicon&s=25 irong (Guest)
on 2006-05-17 12:13
Thks Dan and jonathan, i look your solution.
96e719b743c8cd7bd40ee8dbf936d86e?d=identicon&s=25 irong (Guest)
on 2006-05-17 12:33
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
9d7d8ef2179661d6b30e180fa588cd45?d=identicon&s=25 Calle Dybedahl (Guest)
on 2006-05-17 12:47
(Received via mailing list)
>>>>> "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
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-05-17 14:39
(Received via mailing list)
string = '18052006'
Date.new(*string.scan(/(\d{2})(\d{2})(\d{4})/).flatten.reverse.map(&:to_i))

-Jonathan.
96e719b743c8cd7bd40ee8dbf936d86e?d=identicon&s=25 irong (Guest)
on 2006-05-18 01:17
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.
This topic is locked and can not be replied to.