calabi
calabi

Reputation: 303

Finding start and end date of month

I found code example for finding the start and end day of the current month I'm in. I tried to jump start this code, but am having a hard time figuring out what I have gone wrong.

month = params[:month].to_i
date_start = DateTime.new Date.today.year, params[:month], 1
date_end = (date_start >> 1) + 1  # will add a month and a day
@trips = Trip.all :date => date_start..date_end

I'm not 100% sure what to feed into params. Hope someone can help.

Upvotes: 0

Views: 109

Answers (2)

Manish
Manish

Reputation: 111

params should contains month(numeric) i.e between 1 - 12

For e.g params = {:month => '4'}

Also in second line use month instead of params[:month]

Upvotes: 0

Francisco Soto
Francisco Soto

Reputation: 10392

I am not sure if I understand your question correctly, maybe what you need is this? :

month = 9
date_start = Date.new(Time.now.year, month, 1)
date_end = date_start.next_month - 1

Upvotes: 1

Related Questions