LoginSignup
2
3

More than 5 years have passed since last update.

指定した年度の月の一覧を取得する

Last updated at Posted at 2015-09-03

date_helperは、months_between関数が追加される。指定した月の間のDateオブジェクトを返す。

Gemfileに追加

gem 'date_helper'
$ bundle install

サンプルコード

year = 2015
result = Date.months_between(
    Date.new(year, 4).beginning_of_month,
    Date.new(year + 1, 3).end_of_month
)
result.each{|v| puts v.strftime("%Y-%m")}

出力結果

2015-04
2015-05
2015-06
2015-07
2015-08
2015-09
2015-10
2015-11
2015-12
2016-01
2016-02
2016-03
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3