{"id":13411741,"url":"https://github.com/moneybird/active-date-range","last_synced_at":"2025-06-14T04:34:34.214Z","repository":{"id":43661122,"uuid":"342543940","full_name":"moneybird/active-date-range","owner":"moneybird","description":"Powerful DateRanges for Ruby and ActiveSupport","archived":false,"fork":false,"pushed_at":"2024-07-05T14:03:27.000Z","size":160,"stargazers_count":51,"open_issues_count":1,"forks_count":0,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-10-03T17:18:50.071Z","etag":null,"topics":["activesupport","date","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/moneybird.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-26T10:40:00.000Z","updated_at":"2024-09-11T13:01:02.000Z","dependencies_parsed_at":"2024-10-26T07:58:18.811Z","dependency_job_id":"76f737b6-0493-49e1-aa94-177569b4f2d9","html_url":"https://github.com/moneybird/active-date-range","commit_stats":{"total_commits":99,"total_committers":2,"mean_commits":49.5,"dds":"0.010101010101010055","last_synced_commit":"d9066d0d3bbb8ebeda39889175b4d71fc6e4a591"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moneybird%2Factive-date-range","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moneybird%2Factive-date-range/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moneybird%2Factive-date-range/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moneybird%2Factive-date-range/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moneybird","download_url":"https://codeload.github.com/moneybird/active-date-range/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247838448,"owners_count":21004580,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activesupport","date","ruby","ruby-on-rails"],"created_at":"2024-07-30T20:01:16.388Z","updated_at":"2025-04-11T15:40:26.485Z","avatar_url":"https://github.com/moneybird.png","language":"Ruby","readme":"# Active Date Range\n\n`ActiveDateRange` provides a range of dates with a powerful API to manipulate and use date ranges in your software. Date ranges are commonly used in reporting tools, but can be of use in many situation where you need for example filtering or reporting.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'active_date_range'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install active_date_range\n\n## Usage\n\n### Initialize a new date range\n\nInitialize a new range:\n\n```ruby\nActiveDateRange::DateRange.new(Date.new(2021, 1, 1), Date.new(2021, 12, 31))\nActiveDateRange::DateRange.new(Date.new(2021, 1, 1)..Date.new(2021, 12, 31))\n```\n\nYou can also use shorthands to initialize a range relative to today. Shorthands are available for `this`, `prev` and `next` for the ranges `month`, `quarter`, `year` and `week`:\n\n```ruby\nActiveDateRange::DateRange.this_month\nActiveDateRange::DateRange.this_year\nActiveDateRange::DateRange.this_quarter\nActiveDateRange::DateRange.this_week\nActiveDateRange::DateRange.prev_month\nActiveDateRange::DateRange.prev_year\nActiveDateRange::DateRange.prev_quarter\nActiveDateRange::DateRange.prev_week\nActiveDateRange::DateRange.next_month\nActiveDateRange::DateRange.next_year\nActiveDateRange::DateRange.next_quarter\nActiveDateRange::DateRange.next_week\n```\n\nThe third option is to use parse:\n\n```ruby\nActiveDateRange::DateRange.parse('202101..202112')\nActiveDateRange::DateRange.parse('20210101..20210115')\n```\n\nParse accepts three formats: `YYYYMM..YYYYMM`, `YYYYMMDD..YYYYMMDD` and any short hand like `this_month`.\n\n### The date range instance\n\nA `DateRange` object is an extension of a regular `Range` object. You can use [all methods available on `Range`](https://ruby-doc.org/core-3.0.0/Range.html):\n\n```ruby\ndate_range = ActiveDateRange::DateRange.parse('202101..202112')\ndate_range.begin                            # =\u003e Date.new(2021, 1, 1)\ndate_range.end                              # =\u003e Date.new(2021, 12, 31)\ndate_range.cover?(Date.new(2021, 2, 1))     # =\u003e true\n```\n\n`DateRange` adds extra methods to work with date ranges:\n\n```ruby\ndate_range.days                             # =\u003e 365\ndate_range.months                           # =\u003e 12\ndate_range.quarters                         # =\u003e 4\ndate_range.years                            # =\u003e 1\ndate_range.one_month?                       # =\u003e false\ndate_range.one_week?                        # =\u003e false\ndate_range.one_year?                        # =\u003e true\ndate_range.full_year?                       # =\u003e true\ndate_range.same_year?                       # =\u003e true\ndate_range.before?(Date.new(2022, 1, 1))    # =\u003e true\ndate_range.after?(ActiveDateRange::DateRange.parse('202001..202012')) # =\u003e true\ndate_range.granularity                      # =\u003e :year\ndate_range.to_param                         # =\u003e \"202101..202112\"\ndate_range.to_param(relative: true)         # =\u003e \"this_year\"\n```\n\nYou can also do calculations with the ranges:\n\n```ruby\ndate_range.previous                             # =\u003e DateRange.parse('202001..202012')\ndate_range.previous(2)                          # =\u003e DateRange.parse('201901..202012')\ndate_range.next                                 # =\u003e DateRange.parse('202201..202212')\ndate_range + DateRange.parse('202201..202202')  # =\u003e DateRange.parse('202101..202202')\ndate_range.in_groups_of(:month)                 # =\u003e [DateRange.parse('202101..202101'), ..., DateRange.parse('202112..202112')]\ndate_range.intersection(DateRange.parse('202101..202102')) # =\u003e DateRange.parse('202101..202102')\n```\n\nSupport for boundless ranges is also available:\n\n```ruby\ndate_range = DateRange.parse('202101..')\ndate_range.boundless? # =\u003e true\ndate_range.in_groups_of(:month) # =\u003e Enumerator::Lazy\nModel.where(date: date_range) # =\u003e SQL \"WHERE date \u003e= 2021-01-01\"\n```\n\nAnd lastly you can call `.humanize` to get a localizable human representation of the range for in the user interface:\n\n```ruby\ndate_range.humanize                      # =\u003e '2021'\ndate_range.humanize(format: :explicit)   # =\u003e 'January 1st, 2021 - December 31st 2021'\n```\n\nSee [active_date_range/locale/en.yml](https://github.com/moneybird/active-date-range/blob/main/lib/active_date_range/locale/en.yml) for all the I18n keys you need to translate for your application.\n\n### ActiveModel type\n\nDate ranges are also available as an ActiveModel type. So you can use a date range attribute and the value will automatically be converted:\n\n```ruby\nclass Report\n  include ActiveModel::Attributes\n\n  attribute :period, :date_range\nend\n```\n\n### Usage example\n\nUse the shorthands to link to a specific period:\n\n```ruby\n\u003c%= link_to \"Show report for #{DateRange.this_year.humanize}\", report_url(period: DateRange.this_year.to_param(relative: true)) %\u003e\n```\n\nBecause we use `to_params(relative: true)`, the user gets a bookmarkable URL which always points to the current year. If you need the URL to be bookmarkable and always point to the same period, remove `relative: true`.\n\nIn your controller, use the parameter in your queries:\n\n```ruby\ndef report\n  @period = DateRange.parse(params[:period])\n  @data = SomeModel.where(date: @period)\nend\n```\n\nIn the report view, use the period object to render previous and next links:\n\n```ruby\n\u003c%= link_to \"Next period\", report_url(period: @period.next) %\u003e\n\u003c%= link_to \"Previous period\", report_url(period: @period.previous) %\u003e\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/moneybird/active-date-range.\n\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoneybird%2Factive-date-range","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoneybird%2Factive-date-range","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoneybird%2Factive-date-range/lists"}