Module: MonthsCaluculator

Defined in:
lib/months_caluculator.rb,
lib/months_caluculator/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.descriptionObject

Your code goes here…



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

def self.description
  p "To Caluculate Exact Number of Months We Will Use this gem"
end

.get_exact_number_of_months(from_date, to_date) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/months_caluculator.rb', line 9

def self.get_exact_number_of_months(from_date, to_date)
  number_of_months = 0
  if from_date.blank? && to_date.blank?
    raise "Please Enter From and To Dates"
  elsif from_date.blank?
    raise "Please Enter From Date"
  elsif to_date.blank?
    raise "Please Enter To Date"
  elsif from_date.present?  && to_date.present?
    from_date = from_date.to_date
    to_date =   to_date.to_date
    number_of_months = (to_date.year*12+to_date.month) - (from_date.year*12+from_date.month)
    end_date = from_date.to_date + number_of_months.months
    number_of_days =  to_date.day-end_date.day
    # If the number of days in month is more than 15 then we will assume it as one month and we add it below.
    number_of_months = number_of_months + 1  if number_of_days >=15
  end
  number_of_months 
end