[ACCEPTED]-Date range in an array, ruby-ruby-on-rails-3

Accepted answer
Score: 78

I don't know if I completely understood 7 the question, but here's an answer that 6 might help

(1.month.ago.to_date..Date.today).map{ |date| date.strftime("%b %d") }

outputs

["Jan 07", "Jan 08", "Jan 09", "Jan 10", "Jan 11", "Jan 12", "Jan 13", "Jan 14", "Jan 15", "Jan 16", "Jan 17", "Jan 18", "Jan 19", "Jan 20", "Jan 21", "Jan 22", "Jan 23", "Jan 24", "Jan 25", "Jan 26", "Jan 27", "Jan 28", "Jan 29", "Jan 30", "Jan 31", "Feb 01", "Feb 02", "Feb 03", "Feb 04", "Feb 05", "Feb 06"] 

You can create a range 5 of dates, and then convert them to the desired 4 format using strftime

Just make sure you use Date objects 3 on your range instead of Time objects, otherwise 2 you will create an array of every second 1 included in that lapse.

Score: 11
require 'date'
now = Date.today
p (now<<1 .. now).map{ |day| day.strftime("%b %-e") }
# No railsy .month.ago.to_date silliness!
# the dash in `%-e` gets rid of the occasional extra space. Credit @Grizz in the comments.

Output:

["Jan 7", "Jan 8", "Jan 9", "Jan 10", (...), "Feb 7"]

0

Score: 0
start = "01/09/2021"
finish = "31/09/2021"
(start.to_date..finish.to_date).map(&:to_s)

0

More Related questions