Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/extended_date.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_day_of_month_for(dat, day) ⇒ Object



4
5
6
7
# File 'lib/extended_date.rb', line 4

def self.first_day_of_month_for(dat, day)
  parsed_date = Date.parse(dat.to_s)
  parsed_date.first_day_of_month(day)
end

Instance Method Details

#beginning_of_monthObject



9
10
11
# File 'lib/extended_date.rb', line 9

def beginning_of_month
  Date.parse(self.strftime("%Y-%m-1"))
end

#first_day_of_month(day) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/extended_date.rb', line 13

def first_day_of_month(day)
  req_wday = case 
    when ["sunday", "0"].include?(day.to_s.downcase) then 0
    when ["monday", "1"].include?(day.to_s.downcase) then 1
    when ["tuesday", "2"].include?(day.to_s.downcase) then 2
    when ["wednesday", "3"].include?(day.to_s.downcase) then 3
    when ["thursday", "4"].include?(day.to_s.downcase) then 4
    when ["friday", "5"].include?(day.to_s.downcase) then 5
    when ["saturday", "6"].include?(day.to_s.downcase) then 6
    else "Unknown"
  end

  if req_wday == "Unknown"
    result = "Invalid Input"
  else
    req_days = (req_wday - beginning_of_month.wday)
    req_days = ((req_days>=0) ? req_days : (7+req_days))
    result = beginning_of_month + req_days
  end
end