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

  • Home
  • SEARCH
  • 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 8994347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:24:40+00:00 2026-06-15T23:24:40+00:00

I need to loop through several months in a Ruby on Rails application. For

  • 0

I need to loop through several months in a Ruby on Rails application. For each month, I need to find the first and last day of that given month. These dates are then used in a separate query to find events occurring during those dates and running calculations on them.

Initially, I tried something like:

(11.months.ago.to_date.month..Date.today.month).each do |m|
  start_date = '01-#{m}-#{Date.today.year}'.to_date.beginning_of_month
  end_date = '01-#{m}-#{Date.today.year}'.to_date.end_of_month
end

Of course, the year isn’t updated in this case in the event that 11 months ago involves going back to the previous year. And, I don’t think this type of for/each loop works. I also tried mapping the numbers to an array and using the same method, but received an error.

What’s the best way to accomplish something like this?

  • 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-15T23:24:41+00:00Added an answer on June 15, 2026 at 11:24 pm

    First, count the number of months between two dates (courtesy of Massimiliano Peluso):

    start_date = 13.months.ago.to_date
    # => Wed, 16 Nov 2011
    
    end_date = Date.today
    # => Sun, 16 Dec 2012
    
    number_of_months = (end_date.year*12+end_date.month)-(start_date.year*12+start_date.month)
    # => 13
    

    Then from the start month, counting each month thereafter, find the first/last dates and append to an accumulator array.

    dates = number_of_months.times.each_with_object([]) do |count, array|
      array << [start_date.beginning_of_month + count.months,
                start_date.end_of_month + count.months]
    end
    # => ...
    

    Now dates will contain an array with nested Date pairs corresponding to the first and last date of each month. This dates array will be easy to iterate for processing.

    dates
    # => [[Tue, 01 Nov 2011, Wed, 30 Nov 2011], [Thu, 01 Dec 2011, Fri, 30 Dec 2011], ...
    
    dates[0].first
    # => Tue, 01 Nov 2011
    
    dates[0].last
    # => Wed, 30 Nov 2011
    
    dates[0].last.class
    # => Date
    

    This is tested and working in Rails 3.2.5/Ruby 1.9.3p194

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

Sidebar

Related Questions

I need to loop through each day of the week (monday tues...) and compare
I have a nested JSON object that I need to loop through, and the
I have a foreach loop in jQuery going through each 'div.panel', I need to
I need to loop through a List of Dictionaries List<Dictionary<string,string>> to populate a DataTable.
I need to loop through a set of 2-dimensional array of hidden input fields
i have the following code ..i need to loop through end of the file
Okay so this is a bit complex but I need to loop through this
my heading kinda explains the problem. I need to loop through multiple ul tags
I need to periodically loop through links in my PHP database to check whether
Here's what I need to do: 1) Loop through every cell in a worksheet

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.