26

I was wondering how I can go about creating an arrary of the last month of days "Jan 21" style etc. automatically in ruby (in rails3)?

If today is Feb 6th, then the arrary would have ["Jan 6", "Jan 7"..."Feb 6"]

2
  • I don't understand the question. Does the answer depend on the current date? Some other input? What would the output look like? Perhaps you should provide some sample input and output, and think about whether someone who doesn't know anything about your problem would understand the question. Commented Feb 7, 2011 at 3:22
  • Sorry about the vagueness! updated the question :) Commented Feb 7, 2011 at 3:26

3 Answers 3

78

I don't know if I completely understood the question, but here's an answer that 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 of dates, and then convert them to the desired format using strftime

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

Sign up to request clarification or add additional context in comments.

3 Comments

NB: just to emphasise that you need to beware operating on a Time range, so be sure to convert to Date format as seen above. Thanks.
You need to make sure you include activesupport, require 'active_support/all', if you're not in rails3.
Where is "1" coming from? Where is .month. coming from? are those in activesupport? Is the '1.month.ago' thing a Rails thing?
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"]

3 Comments

What does the << do with respect to DateTime? It looks like "go back a month" but that syntax is very opaque, and I can't find documentation for it wrt the DateTime class
DateTime inherits from Date (Date is listed as parent in the docs). The << method is documented there. It does go back a month indeed.
If you use a - with the %e it won't pad the single digit days with that extra space, and eliminates the need for the .squeeze method. So it should be "%b %-e".
0
start = "01/09/2021"
finish = "31/09/2021"
(start.to_date..finish.to_date).map(&:to_s)

1 Comment

This could do with some explanation of how it answers the question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.