List a last day of the month, one-liner

Install perl module Date::Manip
yum -y install perl-Date-Manip
Explanation:
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 for 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.

Usage:
perl -MDate::Manip -le 'print UnixDate DateCalc(ParseDate("2016-02"), "+1m -1d"), "%e"'
With arguments:
perl -MDate::Manip -le 'print UnixDate DateCalc(ParseDate("@ARGV"), "+1m -1d"), "%e"'

No comments: