【问题标题】:Rails 5 - Set time to 10th of current monthRails 5 - 将时间设置为当月的 10 日
【发布时间】:2017-04-19 00:17:26
【问题描述】:

如何以最简单/最好的方式定义变量,例如当月 10 日?

我有几种方式,比如beginning_of_month + 9.days

另外我可能想将它设置为上个月的 10 号,所以我可以这样做 prev_month.beginning_of_month + 9.days

但我希望为此找到一个本机函数。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5


    【解决方案1】:

    你可以使用days_since:

    2.4.0 :001 > Date.today.beginning_of_month
     => Sat, 01 Apr 2017 
    2.4.0 :004 > Date.today.beginning_of_month.days_since(9)
     => Mon, 10 Apr 2017
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多