arveltのソフトウェア技術メモ

Arvelt's software technology memo

Rubyで現在月の日付を表示する

Rubyで現在月の日付を表示する。バージョンは1.9.2

1.現在月1日を取得
2.現在月末日を取得
3.1から2の日まで繰り返し表示

require 'date'

now = Time.now
firstday = Date.new(now.year,now.month,1)
lastday  = Date.new(now.year,now.month,-1)

puts 'Show days in this month'
for date  in firstday.day..lastday.day do
    puts Date.new(now.year,now.month,date)
end

実行結果

Show days in this month
2011-12-01
2011-12-02
2011-12-03
2011-12-04
2011-12-05
2011-12-06
2011-12-07
2011-12-08
2011-12-09
2011-12-10
2011-12-11
2011-12-12
2011-12-13
2011-12-14
2011-12-15
2011-12-16
2011-12-17
2011-12-18
2011-12-19
2011-12-20
2011-12-21
2011-12-22
2011-12-23
2011-12-24
2011-12-25
2011-12-26
2011-12-27
2011-12-28
2011-12-29
2011-12-30
2011-12-31