bjork24
bjork24

Reputation: 3183

Create a datetime in Rails using month, day, and year

I have params[:month,:day,:year] and I need to convert them into a DateTime that I can place in a hidden input.

What's the best way to do this in Rails 3?

Upvotes: 37

Views: 36035

Answers (2)

Mauricio
Mauricio

Reputation: 5853

You can do

DateTime.new(params[:year], params[:month], params[:day])

Upvotes: 70

Phrogz
Phrogz

Reputation: 303168

Use DateTime.civil:

require 'date'
date = DateTime.civil( *params.values_at( :year, :month, :day ) )

Upvotes: 12

Related Questions