Hard: construct a date for the first day of the target month, then add days
unitil the month number changes. Then go back one.
I think I know which option you’ll choose.
except you have to handle december specially - else you’ll end up with
the
with the wrong year.
easiest is probably:
harp:~ > cat a.rb
require ‘date’
class Date
def self.last_day_of_the_month yyyy, mm
d = new yyyy, mm
d += 42 # warp into the next month
new(d.year, d.month) - 1 # back off one day from first of that
month
end
end
Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.
Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.
exactly
the point is that it always lands into the next month.
Or the month after that - depending on where you start.
Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.
exactly
the point is that it always lands into the next month.
Or the month after that - depending on where you start.
sure - but it always starts on the first day of the month:
require ‘date’
class Date
def self.last_day_of_the_month yyyy, mm
d = new yyyy, mm # no day means the first
one
d += 42 # always the next month
new(d.year, d.month) - 1
end
end
the point is that it always lands into the next month.
Or the month after that - depending on where you start.
sure - but it always starts on the first day of the month:
Yes, of course. I didn’t want to insunuate you code would not do what
you
claimed it to do. It was merely a silly remark, probably stirred up by
this
strange number that I haven’t seen in a while. I am sorry.
Ok, I know that I am going to be smacked for this, but we DO know how
many days are in each month. Why not just have one for february with
considerations for leap year, and lists for those with 30 and 31 days,
or possibly 30 days and everything else has 31. It is nothing more than
a lookup.