I have some forms on my site that use text boxes to enter dates. They typically default to today''s date, in the format "MM/DD/YYYY" However, sometimes a user will erase the box and type in a date, but instead of typing 02/01/2006, they''ll type 2/1/06. Once the record is created in the mysql database, it reads as ''0006-02-01'' instead of ''2006-02-01.'' Is there an easy way around this, or do I need something in my create / update functions to catch and fix this? I have my reasons for not using a date-picker control.
Use a before_save filter and pass your incoming date field to ParseDate module to get a standardized date format suitable for your database. Bob -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Dylan Markow Sent: Wednesday, February 01, 2006 11:06 AM To: rails@lists.rubyonrails.org Subject: [Rails] Dates entered as MM/DD/YY I have some forms on my site that use text boxes to enter dates. They typically default to today''s date, in the format "MM/DD/YYYY" However, sometimes a user will erase the box and type in a date, but instead of typing 02/01/2006, they''ll type 2/1/06. Once the record is created in the mysql database, it reads as ''0006-02-01'' instead of ''2006-02-01.'' Is there an easy way around this, or do I need something in my create / update functions to catch and fix this? I have my reasons for not using a date-picker control. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Dylan Markow <dylan@...> writes:> I have some forms on my site that use text boxes to enter dates. They > typically default to today''s date, in the format "MM/DD/YYYY"> However, sometimes a user will erase the box and type in a date, but > instead of typing 02/01/2006, they''ll type 2/1/06. Once the record is > created in the mysql database, it reads as ''0006-02-01'' instead of > ''2006-02-01.'' Is there an easy way around this, or do I need something > in my create / update functions to catch and fix this? I have my reasons > for not using a date-picker control.You can fix the input before saving with: Date.parse(params[:date], comp=true).to_s http://www.ruby-doc.org/core/classes/Date.html#M001197