指定两个月之间距离的铁路迁移..?

发布于 2024-09-01 00:47:21 字数 225 浏览 1 评论 0原文

我想做两个下拉菜单。开始时间和结束时间。具体来说,我只需要几个月。例如,我想选择一月,然后选择三月,然后让数据库读取到这是这两个月加上二月。

有没有可行的开箱即用的迁移?

我猜..

脚本/生成迁移 AddMonthsToClass beginDate:datetime #through endDate:datetime

如果我的问题听起来迟钝,我提前道歉!对不起! :D

I would like to make two drop downs. a start time, and an end time. Specifically, I only need months. I would like to, for example, choose January, and then March, and then have the database read that it is the these two months plus February.

Is there any out of the box migration that could work?

I'm guessing..

script/generate migration AddMonthsToClass beginDate:datetime #through endDate:datetime

I apologize ahead of time if my question sounds retarded! Sorry! :D

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

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

发布评论

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

评论(1

記柔刀 2024-09-08 00:47:21

这可能会对您有所帮助: http://www.francisfish.com/getting_the_number_of_months_ Between_two_dates_in_rubyrails.htm

获取月份数,您可以从中获取月份名称:

months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
months[start_date.month, number_of_months]

例如
月[时间.现在.月, 5]
=> [“五月”、“六月”、“七月”、“八月”、“九月”]

编辑:
相反,您可以使用:

Date::MONTHNAMES[start_date.month, number_of_months]

编辑:
或者,如果您在选择标签中获取月份数字,则可以使用:

Date::MONTHNAMES[start_month_num, end_month_num]

但是,如果 end_month_num 小于 start_month_num 这将失败这

应该有效:

if start_month <= end_month
  Date::MONTHNAMES[start_month..end_month]
else
  Date::MONTHNAMES[end_month..12] + Date::MONTHNAMES[1..start_month]
end

This may help you: http://www.francisfish.com/getting_the_number_of_months_between_two_dates_in_rubyrails.htm

After you get the number of months, you can get the month names from this:

months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
months[start_date.month, number_of_months]

eg
months[Time.now.month, 5]
=> ["May", "June", "July", "August", "September"]

edit:
Instead of that, you can use:

Date::MONTHNAMES[start_date.month, number_of_months]

edit:
Or if you get the month numbers in your select tag, you can just use:

Date::MONTHNAMES[start_month_num, end_month_num]

but, this would fail if end_month_num is less than start_month_num

This should work:

if start_month <= end_month
  Date::MONTHNAMES[start_month..end_month]
else
  Date::MONTHNAMES[end_month..12] + Date::MONTHNAMES[1..start_month]
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文