是否可以在 Rails 中的日期和月份中添加前导零?日期选择和日期时间选择?

发布于 2024-11-19 01:02:26 字数 1150 浏览 4 评论 0原文

我试图在 date_select 中的日期和月份中添加前导零,对于我在文档中看到的所有内容,您只能指定 :use_month_numbers:use_short_month 但不是 :leading_zeros =>;正确。

查看rails代码,我发现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

一行:

leading_zeros = options.delete(:leading_zeros)

在 DateTimeSelector 下的第 856 行,有这样 我的印象是,即使您设法覆盖第一个方法,并使用 leading_zeros => 调用 DateTimeSelector , true,它只会结束你的聚会,所以不可能这样做。

现在,我的 ruby​​-fu/rails-fu 并不是那么传奇,所以我想知道 – 我可以覆盖我的 Rails 应用程序中的类,仅重新实现我想要的方法以支持leading_zeros,或者什么是最好的方法如何让它发挥作用?

编辑:如果有人问为什么他甚至需要前导零?,好吧,这不是一个交易破坏者,也不是必要,但我希望能够这样做......只是为了整个应用程序的一致性。日期在应用程序和报告中以日/月/年格式显示,但在创建记录时,用户会发现不同的格式。这并不重要,只是一个设计选择。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-11-26 01:02:26

无论如何......我最终通过重载rails应用程序中的方法解决了这个问题(作为测试,不在生产中使用它)。我想我可以关闭它,因为“rails 不支持日期中的前导零”。

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".

辞别 2024-11-26 01:02:26

很简单,只需使用这样的格式:“%-d %-m”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文