class Date
Docs to follow
Attributes
format[RW]
Public Instance Methods
day_of_the_week(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 49 def day_of_the_week(date1, format_string = format) parse_date(date1, format_string).wday end
day_of_the_week_name(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 57 def day_of_the_week_name(date1, format_string = format) Date::DAYNAMES[day_of_the_week(date1, format_string)] end
day_of_the_year(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 65 def day_of_the_year(date1, format_string = format) parse_date(date1, format_string).yday end
days_between(date1, date2, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 69 def days_between(date1, date2, format_string = format) d1 = parse_date(date1, format_string) d2 = parse_date(date2, format_string) d2.mjd - d1.mjd end
dotw(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 45 def dotw(date1, format_string = format) day_of_the_week(date1, format_string) end
dotwn(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 53 def dotwn(date1, format_string = format) day_of_the_week_name(date1, format_string) end
doty(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 61 def doty(date1, format_string = format) day_of_the_year(date1, format_string) end
parse_date(date1, format_string)
click to toggle source
# File lib/dates_toolbox.rb, line 24 def parse_date(date1, format_string) begin return Date.strptime(date1, format_string) rescue ArgumentError => e puts "Invalid date #{date1} with format #{format}" throw e end end
test_format()
click to toggle source
rubocop:enable Lint/DuplicateMethods
# File lib/dates_toolbox.rb, line 20 def test_format (parse_date('1/1/1970', format).wday == 6) end
the_day(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 33 def the_day(date1, format_string = format) parse_date(date1, format_string).day end
the_month(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 37 def the_month(date1, format_string = format) parse_date(date1, format_string).month end
the_year(date1, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 41 def the_year(date1, format_string = format) parse_date(date1, format_string).year end
weekdays(date1, date2, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 90 def weekdays(date1, date2, format_string = format) start_date = parse_date(date1, format_string) end_date = parse_date(date2, format_string) # day of the week in 0-6. Sunday is day-of-week 0; Saturday is day-of-week 6. my_days = (1..5).to_a (start_date..end_date).to_a.select { |k| my_days.include?(k.wday) } end
weekdays_string(date1, date2, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 99 def weekdays_string(date1, date2, format_string = format) weekdays(date1, date2, format_string).map { |n| n.strftime(format) } end
weekends(date1, date2, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 76 def weekends(date1, date2, format_string = format) start_date = parse_date(date1, format_string) end_date = parse_date(date2, format_string) # day of the week in 0-6. Sunday is day-of-week 0; Saturday is day-of-week 6. my_days = [0, 6] (start_date..end_date).to_a.select { |k| my_days.include?(k.wday) } end
weekends_string(date1, date2, format_string = format)
click to toggle source
# File lib/dates_toolbox.rb, line 86 def weekends_string(date1, date2, format_string = format) weekends(date1, date2, format_string).map { |n| n.strftime(format) } end