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 8418989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:28:59+00:00 2026-06-10T02:28:59+00:00

I trying to do a each on a Date interval with Rails 3.2.. something

  • 0

I trying to do a each on a Date interval with Rails 3.2.. something like this:

(1.months.ago.to_date..5.months.from_now.to_date).step(1.month).each do |date|  
    puts date.strftime('%m/%Y')  
end  

But, the step(1.month) does not work.. seems like it get the first month (ex: today is august, it will return jully) and does not iterate the other months..

Is there a way to do that?

Thanks

  • 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-10T02:29:01+00:00Added an answer on June 10, 2026 at 2:29 am

    You are using Date as your iteration base, and 1.month translates (behind the scenes) into seconds I believe.

    When you add to the Date object, it’s in days, thus:

    Date.today + 1 would be tomorrow

    Thus, in your example, you are trying to step 2592000 days.

    What you probably want is something more like:

     (1.months.ago.to_date..5.months.from_now.to_date).step(30).each { |date| puts date.strftime('%m/%Y') }
    

    If you are looking for the iterator to be smart enough to know how many days are in each month when you are “stepping” that’s not going to happen. You will need to roll that on your own.

    You can intelligently iterate through months by using the >> operator, so:

    date = Date.today
    while date < 5.months.from_now.to_date do
         puts date.strftime('%m/%Y')
         date = date>>1
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select the max date in three different fields in each record
I am trying to change each parameter of this http://www.exampleofurl.com/search?q=4&e=25;i=8&b=2 through a loop, but
I'm trying to output each line in a csv file, and it seems like
I have a month and day; I'm trying to work out the next date
I'm trying to write a sql function to do something like: SELECT person.id, person.phonenumber
I have two very similar programs each trying to run two threads OddThread and
I'm trying to extract each page of a PDF as a string: import pyPdf
I'm trying to make each span rotate between a random color on 3sec intervals.
I am trying to retrieve each photo album from facebook by using php. I
I’m trying to convert each integer in a string to its corresponding value in

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.