in reply to Calculating latest 'Sunday' date on the first of the month

Your question is unclear. I think you're asking for the last Sunday before the first day of the current month.

use DateTime qw( ); my $dt = DateTime->today(); $dt->set_day(1); $dt->subtract( days => $dt->day_of_week() ); print($dt->ymd(), "\n");

DateTime

Replies are listed 'Best First'.
Re^2: Calculating latest 'Sunday' date on the first of the month
by Cicatrix (Novice) on Sep 03, 2010 at 06:10 UTC
    thanks ikegami.. its working absolutely fine...
Re^2: Calculating latest 'Sunday' date on the first of the month
by Cicatrix (Novice) on Sep 06, 2010 at 09:54 UTC
    hey that code is again causing problem now... instead of calculating starting of this week its calculating last Sunday i.e, 29/08/2010 as the starting of the week. help!!

      Cicatrix:

      The code ikegami gave you, and the result you reported are what I'd expect from the specifications you gave in your original post. So if you're getting something incorrect, you need to be more clear in specifying the problem.

      Also, you report the problem several days after accepting the answer provided. I suspect that you didn't adequately test the code before reporting success. Normally, you want to test code against all the different types of input cases that will be encountered. So rather than setting $d = DateTime->today() only, you'd initialize $d to various dates, and compare the results of the code with the date you'd expect.

      I suggest you try Test::More and related packages (if you don't use them now), and use them to build tests for code before accepting it. You'll find that the tests, if written well, can also serve as documentation for your code, as it will show future programmers what behaviour to expect for the unusual cases.

      ...roboticus

      I've found your original post very unclear. (It's filled with grammar errors for starters.) I specified what I thought you meant and I provided a solution for that. If you want something else, specify what you want.