【问题标题】:How can I display the days of the week?如何显示星期几?
【发布时间】:2020-10-23 14:12:41
【问题描述】:

我正在尝试列出本周的星期日 - 星期六。 一切似乎都正常,但我似乎从来没有得到我需要的日期。

示例

28 29 30 01 02 03 04 (I need)
28 29 07 08 02 03 04 (what I get)

代码

Sun <%= Date.today.beginning_of_week(:sunday).strftime('%b %d') %>

Mon <%= Date.today.beginning_of_week(:monday).strftime('%b %d') %>

Tue <%= Date.today.end_of_week(:wednesday).strftime('%b %d') %>

Wed <%= Date.today.end_of_week(:thursday).strftime('%b %d') %>

Thu <%= Date.today.end_of_week(:friday).strftime('%b %d') %>

Fri <%= Date.today.end_of_week(:saturday).strftime('%b %d') %>

Sat <%= Date.today.end_of_week.strftime('%b %d') %>

我尝试过同时使用 begin_of_week 和 end_of_week 但不是运气。请帮忙!

【问题讨论】:

    标签: ruby-on-rails ruby date datetime


    【解决方案1】:

    您可以使用范围并遍历:

    <% (Date.today.beginning_of_week(:sunday)...Date.today.end_of_week).each do |date| %>
      <p><%= date.strftime('%a %d') %></p>
    <% end %>
    

    会给你:

    <p>Sun 28</p>
    <p>Mon 29</p>
    <p>Tue 30</p>
    <p>Wed 01</p>
    <p>Thu 02</p>
    <p>Fri 03</p>
    <p>Sat 04</p>
    

    请注意,我们使用... 而不是.. 作为范围。

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 2012-11-08
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多