开发者

Is it possible to add leading zeros to the day and month in rails' date_select and datetime_select?

开发者 https://www.devze.com 2023-03-18 10:02 出处:网络
I\'m trying to add leading zeros to the days and months in a date_select, and for all I can see in the docs you can only specify :use_month_numbers and :use_short_month

I'm trying to add leading zeros to the days and months in a date_select, and for all I can see in the docs you can only specify :use_month_numbers and :use_short_month but not :leading_zeros => true.

Looking at the rails code, I found that the following code in date_helper.rb:

File: actionpack/lib/action_view/helpers/date_helper.rb

def select_day
  if @options[:use_hidden] || @options[:discard_day]
    build_hidden(:day, day)
  else
    build_options_and开发者_运维知识库_select(:day, day, :start => 1, :end => 31, :leading_zeros => false)
  end
end

And in line 856 under DateTimeSelector, there's a line that goes:

leading_zeros = options.delete(:leading_zeros)

So I get the impression that even if you managed to override the first method, and called DateTimeSelector with leading_zeros => true, that it would just end your party there, so it's not possible to do this this way.

Now, my ruby-fu / rails-fu is not that legendary so I was wondering – could I override the class in my rails app reimplementing only the method(s) I want in order to support leading_zeros, or what would be the best way way to get this to work?

EDIT: In case someone's asking why does he even need leading zeroes?, well, it's not a deal breaker, and it's not necessary, but I would like to be able to do this… just for consistency across the app. The dates are displayed with a dd/mm/yy format across app and in reports, but when creating the records the user finds a different format. It's not so important, just a design choice.


Anyway… I finally solved this by overloading the method in the rails app (as a test, not using it in production). I guess I can close this as "rails does not support leading zeros in dates".


It's pretty simple, just use format like this: "%-d %-m"

0

精彩评论

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

关注公众号