Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8554491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:53:40+00:00 2026-06-11T14:53:40+00:00

I have to get all months names in order between two dates using Ruby.

  • 0

I have to get all months names in order between two dates using Ruby. For example, I want to get:

['jan','feb','mars']

When I make a diff between:

1st january and March 15th

I also want it to work when the date diff is greater than one year.

Any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:53:42+00:00Added an answer on June 11, 2026 at 2:53 pm

    I’d go with:

    d1 = Date.parse('jan 1 2011')
    d2 = Date.parse('dec 31 2012')
    
    (d1..d2).map{ |m| m.strftime('%Y%m') }.uniq.map{ |m| Date::ABBR_MONTHNAMES[ Date.strptime(m, '%Y%m').mon ] }
    => ["Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"]
    

    or:

    (d1..d2).map{ |m| m.strftime('%Y%m') }.uniq.map{ |m| Date::ABBR_MONTHNAMES[ m[/\d\d$/ ].to_i ] }
    

    which is probably a little faster.

    The problem is the year boundary. You have to track years and months, not just the months, otherwise you’ll remove all the duplicated month indexes when using uniq to remove the days. I went with the YYYYMM format, to get the right granularity.


    require 'benchmark'
    require 'date'
    
    d1 = Date.parse('jan 1 2011')
    d2 = Date.parse('dec 31 2012')
    
    n = 100
    Benchmark.bm(8) do |x|
      x.report('strptime') { n.times { (d1..d2).map{ |m| m.strftime('%Y%m') }.uniq.map{ |m| Date::ABBR_MONTHNAMES[ Date.strptime(m, '%Y%m').mon ] } } }
      x.report('regex')    { n.times { (d1..d2).map{ |m| m.strftime('%Y%m') }.uniq.map{ |m| Date::ABBR_MONTHNAMES[ m[/\\d\\d$/ ].to_i           ] } } }
    end
    
                  user     system      total        real
    strptime  3.060000   0.020000   3.080000 (  3.076614)
    regex     2.820000   0.010000   2.830000 (  2.829366)
    

    EDIT:

    Let’s make it even more interesting.

    I had some code smell that kept bugging me. I didn’t like using Date.strftime and Date.strptime, so I took another run at the problem: Here are two more solutions that are running a lot faster, along with the benchmarks:

    require 'benchmark'
    require 'date'
    
    def regex_months_between(d1, d2)
      d1, d2 = [d1, d2].map{ |d| Date.parse(d) }.minmax
    
      (d1..d2).map{ |m| m.strftime('%Y%m') }.uniq.map{ |m| Date::ABBR_MONTHNAMES[ m[/\d\d$/ ].to_i ] }
    end
    
    def months_between1(d1, d2)
      d1, d2 = [d1, d2].map{ |d| Date.parse(d) }.minmax
    
      months = (d2.mon - d1.mon) + (d2.year - d1.year) * 12
      month_names = []
      months.times{ |m|
        month_names << Date::ABBR_MONTHNAMES[(d1 >> m).mon]
      }
      month_names << Date::ABBR_MONTHNAMES[d2.mon]
      month_names
    end
    
    def months_between2(d1, d2)
      d1, d2 = [d1, d2].map{ |d| Date.parse(d) }.minmax
    
      months = (d2.mon - d1.mon) + (d2.year - d1.year) * 12
      (d1.mon ... (d1.mon + months)).each_with_object(Date::ABBR_MONTHNAMES[d1.mon, 1]) { |month_offset, month_names_array|
        month_names_array << Date::ABBR_MONTHNAMES[(d1 >> month_offset).mon]
      }
    end
    
    puts regex_months_between('jan 1 2011', 'dec 31 2012').join(', ')
    puts months_between1('jan 1 2011', 'dec 31 2012').join(', ')
    puts months_between2('jan 1 2011', 'dec 31 2012').join(', ')
    
    n = 100
    Benchmark.bm(3) do |b|
      b.report('rmb') { n.times { regex_months_between('jan 1 2011', 'dec 31 2012') } }
      b.report('mb1') { n.times { months_between1('jan 1 2011', 'dec 31 2012') } }
      b.report('mb2') { n.times { months_between2('jan 1 2011', 'dec 31 2012') } }
    end
    

    With output looking like:

    Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
    Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
    Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
            user     system      total        real
    rmb  2.810000   0.010000   2.820000 (  2.820732)
    mb1  0.060000   0.000000   0.060000 (  0.057763)
    mb2  0.060000   0.000000   0.060000 (  0.057112)
    

    Interesting. “rmb” is now running way behind. Pulling it from the tests and bumping up the loops 100x:

    n = 10_000
    Benchmark.bm(3) do |b|
      b.report('mb1') { n.times { months_between1('jan 1 2011', 'dec 31 2012') } }
      b.report('mb2') { n.times { months_between2('jan 1 2011', 'dec 31 2012') } }
    end
    

    Which gives:

            user     system      total        real
    mb1  5.570000   0.060000   5.630000 (  5.615789)
    mb2  5.570000   0.040000   5.610000 (  5.611323)
    

    It’s basically a tie between the two new ways of getting the months. Being anal, I’d go with mb2 because it’d be a little bit faster if I was doing this millions of times, but your mileage might vary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement that i want to get all the system services running
I want to get all the Applications that have intentlisteners to Intent.CATEGORY_HOME so basically
I have the following index I'm creating in order to get all the permissions
I have a query to get all the compatible phones in a specific country,
I need get all items these have no categories int? categoryId = null; var
I have this issue trying to get all the text nodes in an HTML
I have to create an SQL Query to get all rows starting with a
I have a written a method to get all the records and return in
Alright so I have been trying to get all of my separate classes (At
I have a list of parent/child IDs and would like to get all child

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.