Finding the middle day of a month, given any day within that month

Posted on

Problem

Given a date, need to find the middle day of that day’s month, or if there are an even number of days (with no exact middle day), the day should be the closest day to the middle of the month on the second half of the month.

For example, if the date given is August 2 (a month with 31 days), the middle day returned should be Aug 16.
If the date given is September 28 (a month with 30 days), the middle date returned should be 16 (15 would be closest day to middle in first half of month, 16 would be closest day to middle in second half of month).

Here’s the code. Any better way to accomplish this with only version 2.4 and higher standard library and core?

#ruby 2.4.2
#require 'date'
def middleOfMonth(d)
  advancedMonth = d.next_month
  lastDayOfPreviousMonth = (Date.new(advancedMonth.year, advancedMonth.month, 1) - 1).day
  exactMiddle = lastDayOfPreviousMonth.to_f / 2
  actualMiddle = exactMiddle.floor + 1
  actualMiddle
end

Solution

Since February is the only month where the middle is 15 you could just write

d.month == 2 ? 15 : 16

snake_case is more conventional for Ruby variables and functions. lastDayOfPreviousMonth feels misleading: I consider it to be the last day of this month.

Given the last day of the month, you want to map…

28→1529→1530→1631→16

28→1529→1530→1631→16


… to obtain the answer. You should devise a formula that takes advantage of the truncation that naturally occurs with integer division.

Here’s how I would write it:

def middle_of_month(d)
  next_month = d.next_month
  end_of_month = Date.new(next_month.year, next_month.month, 1).prev_day
  end_of_month.day / 2 + 1
end

Since your function accepts a Date object, I would consider it more logical for it to also return a Date object:

def middle_of_month(d)
  next_month = d.next_month
  end_of_month = Date.new(next_month.year, next_month.month, 1).prev_day
  end_of_month.prev_day((end_of_month.day - 1) / 2)
end

Leave a Reply

Your email address will not be published. Required fields are marked *