I and trying to build a calendar in ruby on rails.
我试图在铁轨上用红宝石制作日历。
In ruby-on-rails, monday is considered to be the first day of the week, but i have to set sunday as the first. I intend to change this to properly use the date's built-in methods RoR. Assuming today is sunday, oct 24th. Look the example:
在ruby-on-rails中,星期一被认为是一周的第一天,但我必须将星期日定为第一天。我打算改变它以正确使用日期的内置方法RoR。假设今天是星期天,也就是24日。看看这个例子:
Date.now.beginning_of_week
it prints: 2010-10-18
;but if i could set sunday as the first of the week, it should print : 2010-10-24
它打印:2010-10-18;但如果我可以将星期日定为本周的第一天,它应该打印:2010-10-24
6 个解决方案
#1
21
Nowadays the method accepts the start day param:
现在该方法接受开始日参数:
beginning_of_week(start_day = :monday)
Note: param is supported from Rails 4.2.7 only
注意:仅从Rails 4.2.7支持param
#2
18
I've managed to make a pull request into rails and now you can pass symbol argument to beginning_of_week
method. For example beginning_of_week(:sunday)
will give you a Sunday assuming that weeks starts on Sunday. The same for end_of_week
method. But you have to wait until rails 3.2 release in case you are not on the bleeding edge.
我已经设法向rails发出了一个pull请求,现在你可以将symbol参数传递给beginning_of_week方法。例如,begin_of_week(:sunday)将给你一个星期日,假设星期日星期开始。 end_of_week方法也是如此。但你必须等到导轨3.2释放,以防你不在最前沿。
See this for more info: https://github.com/rails/rails/pull/3547
有关详细信息,请参阅此处:https://github.com/rails/rails/pull/3547
#3
4
You can work within the api by doing the following:
您可以通过执行以下操作在api中工作:
ree-1.8.7-2011.02 :004 > DateTime.now.beginning_of_week
=> Mon, 30 May 2011 00:00:00 -0400
ree-1.8.7-2011.02 :005 > DateTime.now.beginning_of_week.advance(:days => -1)
=> Sun, 29 May 2011 00:00:00 -0400
EDIT:
编辑:
Thinking about this the last couple of minutes, there is good reason for Rails approaching it this way. You have to have a default start of the week, and passing this optional parameter in, you can now store the start of the week on say, the User table in your database and allow the user to pick which day of the week to start their week, eg:
在最后几分钟考虑这个问题,Rails很有理由以这种方式接近它。你必须有一个默认的一周开始,并传递这个可选参数,你现在可以存储一周的开始,比如数据库中的用户表,并允许用户选择一周中的哪一天开始他们的一周,例如:
first_day_of_calendar_week = {:default => 0, :monday => 0, :sunday => -1 ..}
....
@user = User.find_by_some_attribute(...)
DateTime.now.beginning_of_week.advance(
:days => first_day_of_calendar_week[@user.choice] || first_day_of_calendar_week[:default])
#4
2
I don't know if it is something new, but nowadays (Rais 4.2.5 - Ruby 2.2.3) you can set Date.beginning_of_week = :sunday
. I've tried to set this on config/application.rb
but somehow it doesn't work properly, probably it is reset on every new request. Then I put this on a before_action
in my ApplicationController
and works perfectly.
我不知道它是否是新的东西,但现在(Rais 4.2.5 - Ruby 2.2.3)你可以设置Date.beginning_of_week =:sunday。我试图在config / application.rb上设置它,但不知何故它不能正常工作,可能它会在每个新请求上重置。然后我把它放在我的ApplicationController中的before_action并且完美地工作。
#5
0
If you have a look at https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/calculations.rb#L178 you'll see that monday is hardcoded as the first day. Sam's answer is pretty good.
如果您查看https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/calculations.rb#L178,您会看到星期一是硬编码的第一天。山姆的答案非常好。
#6
-2
If you don't want to change the API you can just add '6' days to the date. I'm not sure why you want '6' but that is what you have in your question.
如果您不想更改API,则可以在该日期添加“6”天。我不确定你为什么要'6',但这就是你的问题。
Time.now.beginning_of_week + 6.days
helper.rb
helper.rb
def format_date(time)
time.now.beginning_of_week + 6.days
end
view
视图
<%= format_date(Time.now) %>
#1
21
Nowadays the method accepts the start day param:
现在该方法接受开始日参数:
beginning_of_week(start_day = :monday)
Note: param is supported from Rails 4.2.7 only
注意:仅从Rails 4.2.7支持param
#2
18
I've managed to make a pull request into rails and now you can pass symbol argument to beginning_of_week
method. For example beginning_of_week(:sunday)
will give you a Sunday assuming that weeks starts on Sunday. The same for end_of_week
method. But you have to wait until rails 3.2 release in case you are not on the bleeding edge.
我已经设法向rails发出了一个pull请求,现在你可以将symbol参数传递给beginning_of_week方法。例如,begin_of_week(:sunday)将给你一个星期日,假设星期日星期开始。 end_of_week方法也是如此。但你必须等到导轨3.2释放,以防你不在最前沿。
See this for more info: https://github.com/rails/rails/pull/3547
有关详细信息,请参阅此处:https://github.com/rails/rails/pull/3547
#3
4
You can work within the api by doing the following:
您可以通过执行以下操作在api中工作:
ree-1.8.7-2011.02 :004 > DateTime.now.beginning_of_week
=> Mon, 30 May 2011 00:00:00 -0400
ree-1.8.7-2011.02 :005 > DateTime.now.beginning_of_week.advance(:days => -1)
=> Sun, 29 May 2011 00:00:00 -0400
EDIT:
编辑:
Thinking about this the last couple of minutes, there is good reason for Rails approaching it this way. You have to have a default start of the week, and passing this optional parameter in, you can now store the start of the week on say, the User table in your database and allow the user to pick which day of the week to start their week, eg:
在最后几分钟考虑这个问题,Rails很有理由以这种方式接近它。你必须有一个默认的一周开始,并传递这个可选参数,你现在可以存储一周的开始,比如数据库中的用户表,并允许用户选择一周中的哪一天开始他们的一周,例如:
first_day_of_calendar_week = {:default => 0, :monday => 0, :sunday => -1 ..}
....
@user = User.find_by_some_attribute(...)
DateTime.now.beginning_of_week.advance(
:days => first_day_of_calendar_week[@user.choice] || first_day_of_calendar_week[:default])
#4
2
I don't know if it is something new, but nowadays (Rais 4.2.5 - Ruby 2.2.3) you can set Date.beginning_of_week = :sunday
. I've tried to set this on config/application.rb
but somehow it doesn't work properly, probably it is reset on every new request. Then I put this on a before_action
in my ApplicationController
and works perfectly.
我不知道它是否是新的东西,但现在(Rais 4.2.5 - Ruby 2.2.3)你可以设置Date.beginning_of_week =:sunday。我试图在config / application.rb上设置它,但不知何故它不能正常工作,可能它会在每个新请求上重置。然后我把它放在我的ApplicationController中的before_action并且完美地工作。
#5
0
If you have a look at https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/calculations.rb#L178 you'll see that monday is hardcoded as the first day. Sam's answer is pretty good.
如果您查看https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/calculations.rb#L178,您会看到星期一是硬编码的第一天。山姆的答案非常好。
#6
-2
If you don't want to change the API you can just add '6' days to the date. I'm not sure why you want '6' but that is what you have in your question.
如果您不想更改API,则可以在该日期添加“6”天。我不确定你为什么要'6',但这就是你的问题。
Time.now.beginning_of_week + 6.days
helper.rb
helper.rb
def format_date(time)
time.now.beginning_of_week + 6.days
end
view
视图
<%= format_date(Time.now) %>