I am looking for a Perl script which can give me the last Monday for any specified date.
e.g. For date 2011-06-11, the script should return 2011-06-06
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m assuming that if the given date is a Monday, you want the same date (and not the previous Monday). Here’s one way to do it with DateTime:
(Actually, for the special case of Monday, the
% 7isn’t necessary, because$date->day_of_week - 1will always be 0–6, and that mod 7 is a no-op. But with the% 7, it works for any desired day-of-week, not just Monday.)If you did want the previous Monday, you can change the subtraction:
If you need to parse a date entered on the command line, you might want to look at DateTime::Format::Natural.