Riding The Breeze

Jump to content

Additional information

Categories


Get last day of the month with Ruby

Feb 2007
08

last_day_of_the_month = (Date.parse(year_to_process + "-" + (month_to_process.to_i + 1).to_s + "-01") - 1).mday().to_s rescue "31"

The final rescue is due to catch the error when month_to_process is 12.


4 Comments

  1. # Philippe Rathe on August 31st, 2009

    Won't (month + 1) returns 13 when december!!!!!????????

  2. # hpatoio on September 1st, 2009

    You are right. Just changed the function to works for December too.

  3. # Martin Streicher on January 19th, 2010

    Or do this…

    Date.parse( "#{year}-#{ (month + 1) % 12 }-01" ) - 1

  4. # alain on February 9th, 2010

    Martin: does not work if month = 11…
    (11+1)%12 = 0…


Your Comment

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>




Credits

Template designed by praegnanz.de.