in reply to Finding End of Month's date

I've always used Date::Manip for this task. Remember that the last day of a given month is the day before the first of the next month. If you give Date::Manip a date string consisting of just a month and a year, it'll assume the first of the month. So, all you need to do is advance a month, and back up a day. This can be done easily with Date::Manip, you just add a delta of "+1m -1d" (add a month, subtract a day). Then all you need to do is extract the day of the month from the resulting day. A one-liner, assuming month and year are given as arguments:
perl -MDate::Manip -le \ 'print UnixDate DateCalc(ParseDate("@ARGV"), "+1m -1d"), "%e"'

Replies are listed 'Best First'.
Re^2: Finding End of Month's date
by Aristotle (Chancellor) on Jan 03, 2005 at 12:26 UTC
      I'm well aware of its content.

      I'd still use Date::Manip if I were to calculate it. If it's going to be run as a time critical program, the program would either be written in C, or a lookup instead of a calculation would be used.

        I want to see you get the lookup right for all edge cases.

        Date::Manip is a fine choice if, as the author himself says, you want to allow users to specify dates in fairly freeform, descriptive fashion.

        But other than that, I'd avoid it. It doesn't even make for particularly readable code. The only thing that's intuitive about your snippet is DateCalc()'s offset format. The output format string isn't self-documenting and the implicit truncation is not obvious. I had to read your explanation to know what it does.

        Try this on for size:

        use DateTime; my $date = DateTime ->now ->truncate( to => 'month' ) ->add( months => 1, days => -1 ); print $date->day;

        DateTime rocks.

        Makeshifts last the longest.