Ruby on rails 在rails中打印从1到25以及从上个月26到月末的所选月份日期
@project\u site.attention\u month.strftime(“%m”).to\u i) 这将返回rails中选定的月份整数。Ruby on rails 在rails中打印从1到25以及从上个月26到月末的所选月份日期,ruby-on-rails,Ruby On Rails,@project\u site.attention\u month.strftime(“%m”).to\u i) 这将返回rails中选定的月份整数。 并打印从1到月末日期的选定月份日期 e、 g- 如果@project\u sition.attention\u month.strftime(“%m”).to\u i)值等于1,则从1到31日期打印。 现在我想当@project\u site.attention\u month.strftime(“%m”).to\u i)值为1时,我只想打印所
并打印从1到月末日期的选定月份日期
e、 g- 如果@project\u sition.attention\u month.strftime(“%m”).to\u i)值等于1,则从1到31日期打印。
现在我想当
@project\u site.attention\u month.strftime(“%m”).to\u i)值为1时,我只想打印所选月份的1到25,以及所选月份上一个月的26到结束日期
我当前的代码-
<% (1..(Time.days_in_month @project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
<th class="text-center"><%= date %></th>
<% end %>
例如:
<% (1..(Time.days_in_month 01)).each do |date| %>
<th class="text-center"><%= date %></th>
<% end %>
它将打印1月1日到31日,现在我只想打印1月25日和12月26日到31日。借助这个循环。告诉我这是否是正确的解释
现在,你会得到这样的结果:
<table>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
...
<th>31</th>
<tr>
<table>
<table>
<tr>
<th>26</th> <-- December dates: 26 through end of month
<th>27</th>
<th>28</th>
<th>29</th>
<th>30</th>
<th>31</th>
<th>1</th> <-- January dates: 1 through 25
<th>2</th>
<th>3</th>
<th>4</th>
...
<th>26</th>
<tr>
<table>
这样我们就不必担心闰年、跨越年份等问题。Ruby将为我们解决这些问题
因此,我们希望:
计算今天的日期是在当月25日之前还是之后
- 如果今天的约会很难理解你想要实现什么,因为你太注重细节,而没有告诉我们你想要的是什么。你能给我们举一个日期的例子,以及你期望的结果是什么吗?这段代码也很难闻,因为它在视图中的逻辑性太强了。@max,为了更好地解释,添加了示例。您总是想在第25天切换到下个月吗?@Chiperific,是的
[2020-12-26, 2020-12-27, 2020-12-28, 2020-12-29, ... 2020-01-01, 2020-01-02, 2020-01-03, 2020-01-04, ... 2020-01-25]
class ProjectSite
attr_accessor :attendance_month
# ...
def attendance_date_array
if attendance_month.day <= 25
first_month = attendance_month.beginning_of_month - 1.day
start_day = Date.new(first_month.year, first_month.month, 26)
end_day = Date.new(attendance_month.year, attendance_month.month, 25)
else
start_day = Date.new(attendance_month.year, attendance_month.month, 26)
second_month = attendance_month.end_of_month + 1.day
end_day = Date.new(second_month.year, second_month.month, 25)
end
ary = []
(start_day..end_day).each do |d|
ary << d
end
ary
end
end
<% @project_site.attendance_date_array.each do |date| %>
<th class="text-center"><%= date.day %></th>
<% end %>