I want to create invoices that start on the first day and end on the last day of the month.
Use methods beginning_of_month andend_of_month
beginning_of_month
end_of_month
irb(main):004:0> n = DateTime.now => Wed, 10 May 2017 14:48:01 +0300 irb(main):005:0> n.to_date.beginning_of_month => Mon, 01 May 2017 irb(main):006:0> n.to_date.end_of_month => Wed, 31 May 2017
Given the year and month:
year = 2017 month = 5
You can pass them in Date.new along with the daily value 1 and -1 to get the first and last day respectively:
Date.new
1
-1
require 'date' Date.new(year, month, 1) #=> #<Date: 2017-05-01 ...> Date.new(year, month, -1) #=> #<Date: 2017-05-31 ...>