Is it possible to create a list of months between two dates in Rails

后端 未结 4 435
情书的邮戳
情书的邮戳 2020-12-31 13:38

I am trying to create a page to display a list of links for each month, grouped into years. The months need to be between two dates, Today, and The date of the first entry.<

4条回答
  •  醉梦人生
    2020-12-31 13:50

    Use the date_helper gem which adds the months_between method to the Date class similar to Steve's answer.

    xmas = Date.parse("2013-12-25")
    hksar_establishment_day = Date.parse("2014-07-01")
    
    Date.months_between(xmas,hksar_establishment_day)
     => [Sun, 01 Dec 2013, Wed, 01 Jan 2014, Sat, 01 Feb 2014, Sat, 01 Mar 2014, Tue, 01 Apr 2014, Thu, 01 May 2014, Sun, 01 Jun 2014, Tue, 01 Jul 2014]
    

提交回复
热议问题