开发者

issues with date conversion, using rails 3.0.7 and ruby 1.9.2

i am using datepicker to insert date on my form, which of course returns date to the controller as a string. the issue is when I try to convert this string to date, I get an error "invalid date"

Date.parse(params[:abc][:date])

To verify if I'm doing it right, I wrote the following ruby program:

string_date = "06/18/2011"

date = Date.par开发者_开发问答se(string_date)

puts date

This works perfectly fine. But when I try the same thing in my rails controller, it gives invalid date error.

please help.


When I do this in my Rails 3.1 console:

date = Date.parse("06/18/2011")

I get an "ArgumentError: invalid date" exception. However, it works fine with an ISO-8601 date:

date = Date.parse('2011-06-18')

So perhaps you're having a locale problem. Your script could be using your standard locale setting (which is probably some US locale judging by the date format) but your server is probably using something else.

Try changing the jQuery datepicker date format to something standard and unambiguous by adding this:

dateFormat: 'yy-mm-dd'

to the datepicker's options. The datepicker's default is 'mm/dd/yy' and Ruby's Date class doesn't like seem to like it without a special locale setting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜