Finding the months between two dates in rails











up vote
0
down vote

favorite












I can currently set a time range like so:



start_date: "2018-09-11"
end_date: "2018-11-19"


How can I do this for start to end of months? Examples:



time_range = ["2018-09-11".."2018-09-30"]
time_range = ["2018-10-01".."2018-10-31"]
time_range = ["2018-11-01".."2018-11-19"]









share|improve this question




























    up vote
    0
    down vote

    favorite












    I can currently set a time range like so:



    start_date: "2018-09-11"
    end_date: "2018-11-19"


    How can I do this for start to end of months? Examples:



    time_range = ["2018-09-11".."2018-09-30"]
    time_range = ["2018-10-01".."2018-10-31"]
    time_range = ["2018-11-01".."2018-11-19"]









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I can currently set a time range like so:



      start_date: "2018-09-11"
      end_date: "2018-11-19"


      How can I do this for start to end of months? Examples:



      time_range = ["2018-09-11".."2018-09-30"]
      time_range = ["2018-10-01".."2018-10-31"]
      time_range = ["2018-11-01".."2018-11-19"]









      share|improve this question















      I can currently set a time range like so:



      start_date: "2018-09-11"
      end_date: "2018-11-19"


      How can I do this for start to end of months? Examples:



      time_range = ["2018-09-11".."2018-09-30"]
      time_range = ["2018-10-01".."2018-10-31"]
      time_range = ["2018-11-01".."2018-11-19"]






      ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 at 16:53









      Tim Diekmann

      2,80091633




      2,80091633










      asked Nov 17 at 11:55









      RAM PRATHIP

      54




      54
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I'm not sure what's exactly your desired outcome but, given start date and end date as Date objects, you can perform



          (start_date..end_date).to_a.group_by(&:month).values


          and at the end what you get is a three element array, and each element contains an array with all the dates in that range for a month






          share|improve this answer























          • In Ruby strings (even those representing dates) do not have a method month. Is the situation different in Rails? If "yes", would this not produce an array whose elements (each corresponding to a month) be an array of dates? If so, don't you have a further step to convert each of those arrays of dates to a range (even though you've been awarded the greenie)?
            – Cary Swoveland
            Nov 17 at 20:06












          • @CarySwoveland I started from two date objects
            – Ursus
            Nov 17 at 23:40










          • And it's pure ruby
            – Ursus
            Nov 17 at 23:40










          • up-voting as most relevant answer
            – ray
            2 days ago


















          up vote
          0
          down vote













          I do not know if I understand very well what you asked, but I'll try to help you.



          The Date class has several methods that will help you to work with dates.
          Date < Object



          Examples



          my_date_range_array = [Date.today.beginning_of_year..Date.today.end_of_year]

          my_date_time_range_array = [Time.now.beginning_of_year..Time.now.end_of_year]

          my_date_range_array = [6.months.ago..Date.today]


          YourModel.where date: Date.today.beginning_of_month..Date.today

          YourModel.where date: 6.months.ago..Date.today


          If you need every single date in the range, you can use something like this:



          (Date.today.beginning_of_year..Date.today.end_of_year).map{ |date| date }


          I hope that my answer helps you






          share|improve this answer








          New contributor




          Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

























            up vote
            0
            down vote













            This is a pure Ruby solution, but I believe (though I don't know Rails) it can be simplified slightly by replacing my methods first_day_of_month and first_day_of_month with Rails methods beginning_of_month and end_of_month, respectively. I designed the method for efficiency over simplicity.



            require 'date'

            DATE_FMT = "%Y-%m-%d"

            def date_ranges(start_date_str, end_date_str)
            start_date = Date.strptime(start_date_str, DATE_FMT)
            end_date = Date.strptime(end_date_str, DATE_FMT)
            return [start_date_str..end_date_str] if
            [start_date.year, start_date.month] == [end_date.year, end_date.month]
            d = start_date
            ranges = [start_date_str..last_day_of_month(d)]
            loop do
            d = d >> 1
            break if [d.year, d.month] == [end_date.year, end_date.month]
            ranges << (first_day_of_month(d)..last_day_of_month(d))
            end
            ranges << (first_day_of_month(d)..end_date_str)
            end

            def first_day_of_month(d)
            (d - d.day + 1).strftime(DATE_FMT)
            end

            def last_day_of_month(d)
            ((d >> 1)-d.day).strftime(DATE_FMT)
            end




            date_ranges("2018-09-11", "2019-02-11")
            #=> ["2018-09-11".."2018-09-30", "2018-10-01".."2018-10-31",
            # "2018-11-01".."2018-11-30", "2018-12-01".."2018-12-31",
            # "2019-01-01".."2019-01-31", "2019-02-01".."2019-02-11"]

            date_ranges("2018-09-08", "2018-09-23")
            #=> ["2018-09-08".."2018-09-23"]





            share|improve this answer






























              up vote
              0
              down vote













              With the information provided by the OP, this is what I understand he is looking for.
              Given a set range for example:



               time_range = "2018-09-11".."2018-09-19"

              new_range_min = time_range.min.to_date.beginning_of_month
              new_range_max = time_range.max.to_date.end_of_month
              new_range = new_range_min..new_range_max





              share|improve this answer























              • in Ruby u mean, and no, it does not. The receiver was intended to be a Date object, thanks for pointing out the problem. Updating answer.
                – MrCodeX
                yesterday













              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350984%2ffinding-the-months-between-two-dates-in-rails%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              I'm not sure what's exactly your desired outcome but, given start date and end date as Date objects, you can perform



              (start_date..end_date).to_a.group_by(&:month).values


              and at the end what you get is a three element array, and each element contains an array with all the dates in that range for a month






              share|improve this answer























              • In Ruby strings (even those representing dates) do not have a method month. Is the situation different in Rails? If "yes", would this not produce an array whose elements (each corresponding to a month) be an array of dates? If so, don't you have a further step to convert each of those arrays of dates to a range (even though you've been awarded the greenie)?
                – Cary Swoveland
                Nov 17 at 20:06












              • @CarySwoveland I started from two date objects
                – Ursus
                Nov 17 at 23:40










              • And it's pure ruby
                – Ursus
                Nov 17 at 23:40










              • up-voting as most relevant answer
                – ray
                2 days ago















              up vote
              1
              down vote



              accepted










              I'm not sure what's exactly your desired outcome but, given start date and end date as Date objects, you can perform



              (start_date..end_date).to_a.group_by(&:month).values


              and at the end what you get is a three element array, and each element contains an array with all the dates in that range for a month






              share|improve this answer























              • In Ruby strings (even those representing dates) do not have a method month. Is the situation different in Rails? If "yes", would this not produce an array whose elements (each corresponding to a month) be an array of dates? If so, don't you have a further step to convert each of those arrays of dates to a range (even though you've been awarded the greenie)?
                – Cary Swoveland
                Nov 17 at 20:06












              • @CarySwoveland I started from two date objects
                – Ursus
                Nov 17 at 23:40










              • And it's pure ruby
                – Ursus
                Nov 17 at 23:40










              • up-voting as most relevant answer
                – ray
                2 days ago













              up vote
              1
              down vote



              accepted







              up vote
              1
              down vote



              accepted






              I'm not sure what's exactly your desired outcome but, given start date and end date as Date objects, you can perform



              (start_date..end_date).to_a.group_by(&:month).values


              and at the end what you get is a three element array, and each element contains an array with all the dates in that range for a month






              share|improve this answer














              I'm not sure what's exactly your desired outcome but, given start date and end date as Date objects, you can perform



              (start_date..end_date).to_a.group_by(&:month).values


              and at the end what you get is a three element array, and each element contains an array with all the dates in that range for a month







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 17 at 12:21

























              answered Nov 17 at 12:08









              Ursus

              19.1k31328




              19.1k31328












              • In Ruby strings (even those representing dates) do not have a method month. Is the situation different in Rails? If "yes", would this not produce an array whose elements (each corresponding to a month) be an array of dates? If so, don't you have a further step to convert each of those arrays of dates to a range (even though you've been awarded the greenie)?
                – Cary Swoveland
                Nov 17 at 20:06












              • @CarySwoveland I started from two date objects
                – Ursus
                Nov 17 at 23:40










              • And it's pure ruby
                – Ursus
                Nov 17 at 23:40










              • up-voting as most relevant answer
                – ray
                2 days ago


















              • In Ruby strings (even those representing dates) do not have a method month. Is the situation different in Rails? If "yes", would this not produce an array whose elements (each corresponding to a month) be an array of dates? If so, don't you have a further step to convert each of those arrays of dates to a range (even though you've been awarded the greenie)?
                – Cary Swoveland
                Nov 17 at 20:06












              • @CarySwoveland I started from two date objects
                – Ursus
                Nov 17 at 23:40










              • And it's pure ruby
                – Ursus
                Nov 17 at 23:40










              • up-voting as most relevant answer
                – ray
                2 days ago
















              In Ruby strings (even those representing dates) do not have a method month. Is the situation different in Rails? If "yes", would this not produce an array whose elements (each corresponding to a month) be an array of dates? If so, don't you have a further step to convert each of those arrays of dates to a range (even though you've been awarded the greenie)?
              – Cary Swoveland
              Nov 17 at 20:06






              In Ruby strings (even those representing dates) do not have a method month. Is the situation different in Rails? If "yes", would this not produce an array whose elements (each corresponding to a month) be an array of dates? If so, don't you have a further step to convert each of those arrays of dates to a range (even though you've been awarded the greenie)?
              – Cary Swoveland
              Nov 17 at 20:06














              @CarySwoveland I started from two date objects
              – Ursus
              Nov 17 at 23:40




              @CarySwoveland I started from two date objects
              – Ursus
              Nov 17 at 23:40












              And it's pure ruby
              – Ursus
              Nov 17 at 23:40




              And it's pure ruby
              – Ursus
              Nov 17 at 23:40












              up-voting as most relevant answer
              – ray
              2 days ago




              up-voting as most relevant answer
              – ray
              2 days ago












              up vote
              0
              down vote













              I do not know if I understand very well what you asked, but I'll try to help you.



              The Date class has several methods that will help you to work with dates.
              Date < Object



              Examples



              my_date_range_array = [Date.today.beginning_of_year..Date.today.end_of_year]

              my_date_time_range_array = [Time.now.beginning_of_year..Time.now.end_of_year]

              my_date_range_array = [6.months.ago..Date.today]


              YourModel.where date: Date.today.beginning_of_month..Date.today

              YourModel.where date: 6.months.ago..Date.today


              If you need every single date in the range, you can use something like this:



              (Date.today.beginning_of_year..Date.today.end_of_year).map{ |date| date }


              I hope that my answer helps you






              share|improve this answer








              New contributor




              Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                up vote
                0
                down vote













                I do not know if I understand very well what you asked, but I'll try to help you.



                The Date class has several methods that will help you to work with dates.
                Date < Object



                Examples



                my_date_range_array = [Date.today.beginning_of_year..Date.today.end_of_year]

                my_date_time_range_array = [Time.now.beginning_of_year..Time.now.end_of_year]

                my_date_range_array = [6.months.ago..Date.today]


                YourModel.where date: Date.today.beginning_of_month..Date.today

                YourModel.where date: 6.months.ago..Date.today


                If you need every single date in the range, you can use something like this:



                (Date.today.beginning_of_year..Date.today.end_of_year).map{ |date| date }


                I hope that my answer helps you






                share|improve this answer








                New contributor




                Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I do not know if I understand very well what you asked, but I'll try to help you.



                  The Date class has several methods that will help you to work with dates.
                  Date < Object



                  Examples



                  my_date_range_array = [Date.today.beginning_of_year..Date.today.end_of_year]

                  my_date_time_range_array = [Time.now.beginning_of_year..Time.now.end_of_year]

                  my_date_range_array = [6.months.ago..Date.today]


                  YourModel.where date: Date.today.beginning_of_month..Date.today

                  YourModel.where date: 6.months.ago..Date.today


                  If you need every single date in the range, you can use something like this:



                  (Date.today.beginning_of_year..Date.today.end_of_year).map{ |date| date }


                  I hope that my answer helps you






                  share|improve this answer








                  New contributor




                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  I do not know if I understand very well what you asked, but I'll try to help you.



                  The Date class has several methods that will help you to work with dates.
                  Date < Object



                  Examples



                  my_date_range_array = [Date.today.beginning_of_year..Date.today.end_of_year]

                  my_date_time_range_array = [Time.now.beginning_of_year..Time.now.end_of_year]

                  my_date_range_array = [6.months.ago..Date.today]


                  YourModel.where date: Date.today.beginning_of_month..Date.today

                  YourModel.where date: 6.months.ago..Date.today


                  If you need every single date in the range, you can use something like this:



                  (Date.today.beginning_of_year..Date.today.end_of_year).map{ |date| date }


                  I hope that my answer helps you







                  share|improve this answer








                  New contributor




                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered Nov 17 at 12:38









                  Leonardo da Rosa

                  313




                  313




                  New contributor




                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






















                      up vote
                      0
                      down vote













                      This is a pure Ruby solution, but I believe (though I don't know Rails) it can be simplified slightly by replacing my methods first_day_of_month and first_day_of_month with Rails methods beginning_of_month and end_of_month, respectively. I designed the method for efficiency over simplicity.



                      require 'date'

                      DATE_FMT = "%Y-%m-%d"

                      def date_ranges(start_date_str, end_date_str)
                      start_date = Date.strptime(start_date_str, DATE_FMT)
                      end_date = Date.strptime(end_date_str, DATE_FMT)
                      return [start_date_str..end_date_str] if
                      [start_date.year, start_date.month] == [end_date.year, end_date.month]
                      d = start_date
                      ranges = [start_date_str..last_day_of_month(d)]
                      loop do
                      d = d >> 1
                      break if [d.year, d.month] == [end_date.year, end_date.month]
                      ranges << (first_day_of_month(d)..last_day_of_month(d))
                      end
                      ranges << (first_day_of_month(d)..end_date_str)
                      end

                      def first_day_of_month(d)
                      (d - d.day + 1).strftime(DATE_FMT)
                      end

                      def last_day_of_month(d)
                      ((d >> 1)-d.day).strftime(DATE_FMT)
                      end




                      date_ranges("2018-09-11", "2019-02-11")
                      #=> ["2018-09-11".."2018-09-30", "2018-10-01".."2018-10-31",
                      # "2018-11-01".."2018-11-30", "2018-12-01".."2018-12-31",
                      # "2019-01-01".."2019-01-31", "2019-02-01".."2019-02-11"]

                      date_ranges("2018-09-08", "2018-09-23")
                      #=> ["2018-09-08".."2018-09-23"]





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        This is a pure Ruby solution, but I believe (though I don't know Rails) it can be simplified slightly by replacing my methods first_day_of_month and first_day_of_month with Rails methods beginning_of_month and end_of_month, respectively. I designed the method for efficiency over simplicity.



                        require 'date'

                        DATE_FMT = "%Y-%m-%d"

                        def date_ranges(start_date_str, end_date_str)
                        start_date = Date.strptime(start_date_str, DATE_FMT)
                        end_date = Date.strptime(end_date_str, DATE_FMT)
                        return [start_date_str..end_date_str] if
                        [start_date.year, start_date.month] == [end_date.year, end_date.month]
                        d = start_date
                        ranges = [start_date_str..last_day_of_month(d)]
                        loop do
                        d = d >> 1
                        break if [d.year, d.month] == [end_date.year, end_date.month]
                        ranges << (first_day_of_month(d)..last_day_of_month(d))
                        end
                        ranges << (first_day_of_month(d)..end_date_str)
                        end

                        def first_day_of_month(d)
                        (d - d.day + 1).strftime(DATE_FMT)
                        end

                        def last_day_of_month(d)
                        ((d >> 1)-d.day).strftime(DATE_FMT)
                        end




                        date_ranges("2018-09-11", "2019-02-11")
                        #=> ["2018-09-11".."2018-09-30", "2018-10-01".."2018-10-31",
                        # "2018-11-01".."2018-11-30", "2018-12-01".."2018-12-31",
                        # "2019-01-01".."2019-01-31", "2019-02-01".."2019-02-11"]

                        date_ranges("2018-09-08", "2018-09-23")
                        #=> ["2018-09-08".."2018-09-23"]





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          This is a pure Ruby solution, but I believe (though I don't know Rails) it can be simplified slightly by replacing my methods first_day_of_month and first_day_of_month with Rails methods beginning_of_month and end_of_month, respectively. I designed the method for efficiency over simplicity.



                          require 'date'

                          DATE_FMT = "%Y-%m-%d"

                          def date_ranges(start_date_str, end_date_str)
                          start_date = Date.strptime(start_date_str, DATE_FMT)
                          end_date = Date.strptime(end_date_str, DATE_FMT)
                          return [start_date_str..end_date_str] if
                          [start_date.year, start_date.month] == [end_date.year, end_date.month]
                          d = start_date
                          ranges = [start_date_str..last_day_of_month(d)]
                          loop do
                          d = d >> 1
                          break if [d.year, d.month] == [end_date.year, end_date.month]
                          ranges << (first_day_of_month(d)..last_day_of_month(d))
                          end
                          ranges << (first_day_of_month(d)..end_date_str)
                          end

                          def first_day_of_month(d)
                          (d - d.day + 1).strftime(DATE_FMT)
                          end

                          def last_day_of_month(d)
                          ((d >> 1)-d.day).strftime(DATE_FMT)
                          end




                          date_ranges("2018-09-11", "2019-02-11")
                          #=> ["2018-09-11".."2018-09-30", "2018-10-01".."2018-10-31",
                          # "2018-11-01".."2018-11-30", "2018-12-01".."2018-12-31",
                          # "2019-01-01".."2019-01-31", "2019-02-01".."2019-02-11"]

                          date_ranges("2018-09-08", "2018-09-23")
                          #=> ["2018-09-08".."2018-09-23"]





                          share|improve this answer














                          This is a pure Ruby solution, but I believe (though I don't know Rails) it can be simplified slightly by replacing my methods first_day_of_month and first_day_of_month with Rails methods beginning_of_month and end_of_month, respectively. I designed the method for efficiency over simplicity.



                          require 'date'

                          DATE_FMT = "%Y-%m-%d"

                          def date_ranges(start_date_str, end_date_str)
                          start_date = Date.strptime(start_date_str, DATE_FMT)
                          end_date = Date.strptime(end_date_str, DATE_FMT)
                          return [start_date_str..end_date_str] if
                          [start_date.year, start_date.month] == [end_date.year, end_date.month]
                          d = start_date
                          ranges = [start_date_str..last_day_of_month(d)]
                          loop do
                          d = d >> 1
                          break if [d.year, d.month] == [end_date.year, end_date.month]
                          ranges << (first_day_of_month(d)..last_day_of_month(d))
                          end
                          ranges << (first_day_of_month(d)..end_date_str)
                          end

                          def first_day_of_month(d)
                          (d - d.day + 1).strftime(DATE_FMT)
                          end

                          def last_day_of_month(d)
                          ((d >> 1)-d.day).strftime(DATE_FMT)
                          end




                          date_ranges("2018-09-11", "2019-02-11")
                          #=> ["2018-09-11".."2018-09-30", "2018-10-01".."2018-10-31",
                          # "2018-11-01".."2018-11-30", "2018-12-01".."2018-12-31",
                          # "2019-01-01".."2019-01-31", "2019-02-01".."2019-02-11"]

                          date_ranges("2018-09-08", "2018-09-23")
                          #=> ["2018-09-08".."2018-09-23"]






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 17 at 20:08

























                          answered Nov 17 at 19:53









                          Cary Swoveland

                          66.4k53865




                          66.4k53865






















                              up vote
                              0
                              down vote













                              With the information provided by the OP, this is what I understand he is looking for.
                              Given a set range for example:



                               time_range = "2018-09-11".."2018-09-19"

                              new_range_min = time_range.min.to_date.beginning_of_month
                              new_range_max = time_range.max.to_date.end_of_month
                              new_range = new_range_min..new_range_max





                              share|improve this answer























                              • in Ruby u mean, and no, it does not. The receiver was intended to be a Date object, thanks for pointing out the problem. Updating answer.
                                – MrCodeX
                                yesterday

















                              up vote
                              0
                              down vote













                              With the information provided by the OP, this is what I understand he is looking for.
                              Given a set range for example:



                               time_range = "2018-09-11".."2018-09-19"

                              new_range_min = time_range.min.to_date.beginning_of_month
                              new_range_max = time_range.max.to_date.end_of_month
                              new_range = new_range_min..new_range_max





                              share|improve this answer























                              • in Ruby u mean, and no, it does not. The receiver was intended to be a Date object, thanks for pointing out the problem. Updating answer.
                                – MrCodeX
                                yesterday















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              With the information provided by the OP, this is what I understand he is looking for.
                              Given a set range for example:



                               time_range = "2018-09-11".."2018-09-19"

                              new_range_min = time_range.min.to_date.beginning_of_month
                              new_range_max = time_range.max.to_date.end_of_month
                              new_range = new_range_min..new_range_max





                              share|improve this answer














                              With the information provided by the OP, this is what I understand he is looking for.
                              Given a set range for example:



                               time_range = "2018-09-11".."2018-09-19"

                              new_range_min = time_range.min.to_date.beginning_of_month
                              new_range_max = time_range.max.to_date.end_of_month
                              new_range = new_range_min..new_range_max






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited yesterday

























                              answered Nov 17 at 12:31









                              MrCodeX

                              114




                              114












                              • in Ruby u mean, and no, it does not. The receiver was intended to be a Date object, thanks for pointing out the problem. Updating answer.
                                – MrCodeX
                                yesterday




















                              • in Ruby u mean, and no, it does not. The receiver was intended to be a Date object, thanks for pointing out the problem. Updating answer.
                                – MrCodeX
                                yesterday


















                              in Ruby u mean, and no, it does not. The receiver was intended to be a Date object, thanks for pointing out the problem. Updating answer.
                              – MrCodeX
                              yesterday






                              in Ruby u mean, and no, it does not. The receiver was intended to be a Date object, thanks for pointing out the problem. Updating answer.
                              – MrCodeX
                              yesterday




















                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350984%2ffinding-the-months-between-two-dates-in-rails%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Ottavio Pratesi

                              Tricia Helfer

                              15 giugno