Build Status Software License Release Gem Version Github commits (since latest release) GitHub repo size in bytes GitHub contributors

Dates Toolbox

dates_toolbox is a fully feature gem full of utilities for manipulating dates.

Installation

Add this line to your application's Gemfile:

gem 'dates_toolbox'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dates_toolbox

Usage

Basic

The following methods are available.

  1. the_day = The numeric day part of a given date
  2. the_month = The numeric month part of a given date
  3. the_year = The numeric year part of a given date
  4. day_of_the_week (dotw) = The day of the week for a given date (Sunday = 0, Saturday = 6)
  5. day_of_the_week_name (dotwn) = The name of the day of the week for a given date
  6. day_of_the_year (doty) = The day of the year for a given date
  7. days_between = The number of days between to days (excluding both given days)
  8. weekends = Arrays of Date objects for weekends between given dates (including both given dates)
  9. weekends_string = Array of date strings for weekends between given dates (including both given dates)
  10. weekdays = Arrays of Date objects for weekdays between given dates (including both given dates)
  11. weekdays_string = Array of date strings for weekdays between given dates (including both given dates)

Advanced

All of the methods will default to a UK date format (%d/%m/%Y), however it is also possible to set a custom date format in one of 2 different ways.

d = Date.new

# Override/reset the class level default for all methods.
d.format = '%m-%d-%Y'

# or

# Override the default on a method by method basis
d.the_day('6-23-1912', '%m-%d-%Y')

Examples

d = Date.new

puts d.the_day('23/6/1912')
# or
puts '23/6/1912'.the_day
# 1

puts d.the_month('23/6/1912')
# or
puts '23/6/1912'.the_month
# 6

puts d.the_year('23/6/1912')
# or
puts '23/6/1912'.the_year
# 1912

puts d.day_of_the_week('23/6/1912')
# or
puts '23/6/1912'.day_of_the_week
# 0

puts d.day_of_the_week_name('23/6/1912')
# or
puts '23/6/1912'.day_of_the_week_name
# Sunday

puts d.day_of_the_year('23/6/1912')
# or
puts '23/6/1912'.day_of_the_year
# 175

puts d.days_between('23/6/1912', '7/6/1954')
# or
puts '23/6/1912'.days_between('7/6/1954')
# 15324

puts d.weekends('1/1/2000', '31/1/2000')
# or
puts '1/1/2000'.weekends('31/1/2000')
# Array of date objects

puts d.weekends_string('1/1/2000', '31/1/2000')
# or
puts '1/1/2000'.weekends_string('31/1/2000')
# ["01/01/2000", "02/01/2000", "08/01/2000", "09/01/2000", "15/01/2000", "16/01/2000", "22/01/2000", "23/01/2000", "29/01/2000", "30/01/2000"]

puts d.weekdays('1/1/2000', '15/1/2000')
# or
puts '1/1/2000'.weekdays('15/1/2000')
# Array of date objects

puts d.weekdays_string('1/1/2000', '15/1/2000')
#
puts '1/1/2000'.weekdays_string('15/1/2000')
# ["03/01/2000", "04/01/2000", "05/01/2000", "06/01/2000", "07/01/2000", "10/01/2000", "11/01/2000", "12/01/2000", "13/01/2000", "14/01/2000"]

Method Aliases

  1. day_of_the_week is aliased to dotw
  2. day_of_the_week_name is aliased to dotwn
  3. day_of_the_year is aliased to doty

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To 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.

Testing

For local testing make sure that you run bundle exec rspec spec and then rake install to install the gem locally.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/WolfSoftware/dates_toolbox. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the dates_toolbox project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

To-Do List

  • [ ] current_date/tody
  • [ ] Short/Long day and month name options
  • [ ] get_ordinal
  • [ ] is_valid_date
  • [ ] is_leap_year
  • [ ] days_in_month / days_in_year
  • [ ] week_of_the_year
  • [ ] julian_dte
  • [ ] is_future_date/is_past_date
  • [ ] quarter_of_the_year
  • [ ] end/start of this/last/next week
  • [ ] end/start of this/last/next month
  • [ ] next/prev_weekday
  • [ ] days/weeks ahead/behind
  • [ ] weeks in a month (working weeks)