开发者

Date formats in ActiveRecord / Rails 3

In my model, I have a departure_date and a return_date.

I am using a text_field instead of the date_select so that I can use the JQuery datepicker.

My app is based in the US for now but I do hope to get international members.

So basically this is what is hap开发者_Go百科pening. The user (US) types in a date such as 04/01/2010 (April 1st).

Of course, MySQL stores it as a datetime such as 2010-04-01...

Anyway, when the user goes to edit the date later on, it shows "01/04/2010" because I am using a strftime("%m/%d/%Y) which doesn't make sense....so it thinks it is January 4th instead of the original April 1st.

It's like the only way to accurately store the data is for the user to type in: 2010-04-01

I hope all of this makes sense. What I am really after is a way for the user to type in (or use the datepicker) a date in their native format.

So someone in Europe could type in 01/04/2010 for April 1st but someone in the US would type in 04/01/2010.

Is there an easy, elegant solution to this?

Thanks for any suggestions.


I have been confronted to the same issue lately.

My website is based in france, so i have to store date as a french date.

I also use a Jquery DatePicker.

To do this, i set the I18n.locale to the visitor's country. For exemple :

I18.locale = :fr

You can do this with a before_filter into your application_controller

Then, into your locales/fr.yml you can define the way you want to show the date

And finally, into your view, you can set the date format view, with the JqueryDatePicket properties.

If i remember well, this methode was something like that:

$.datepicker.formatDate('dd-mm-yyyy');

You can find it anyway on the datepicker documentation.

And this format can be found for exemple into your locale/fr.yml

And that's all !

Hope this will help !

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜