Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Get date interval from week of year -> Date::Manip

by LupoX (Pilgrim)
on Sep 08, 2003 at 06:42 UTC ( [id://289695]=perlquestion: print w/replies, xml ) Need Help??

LupoX has asked for the wisdom of the Perl Monks concerning the following question:

Hi people!

currently I use Date::Manip for getting the week of year on a given date.

Now I would like to get the date intervall for a given week of year (always for the current year).
Date::Manip does not seem to provide such a routine.
Now the only way I can imagine to do this is a very ugly one:
Make a loop starting with 01-01-2003, check if given week of year does match the one for the current date, and then take the next date. If it does match store the date, and add 7 days for the last day of the week.

Is there a better way?

Thanks a lot for any suggestions and or comments.
Georg -> perlmonks.week.of.year@viot.de

  • Comment on Get date interval from week of year -> Date::Manip

Replies are listed 'Best First'.
Re: Get date interval from week of year -> Date::Manip
by Thelonius (Priest) on Sep 08, 2003 at 08:05 UTC
    $firstday = Date_GetPrev($date, $fdow, 1);
    where $fdow is 1 for Monday or 7 for Sunday. Add 6 to get the last day of week, not 7.
Re: Get date interval from week of year -> Date::Manip
by Anonymous Monk on Sep 08, 2003 at 07:31 UTC
    use Date::Manip qw[ ParseDate UnixDate ]; use Data::Dumper; $ENV{TZ} = 'PST'; my @date = UnixDate( ParseDate("today"), qw[%Y %m %d %W ] ); print join "|",@date,$/; print join "|",UnixDate( ParseDate("$date[3] week $date[0]"), qw[%Y %m + %d %W ] ),$/; print join "|",UnixDate( ParseDate(1+$date[3]." week $date[0]"), qw[%Y + %m %d %W ] ),$/;
Re: Get date interval from week of year -> Date::Manip
by Roger (Parson) on Sep 08, 2003 at 08:11 UTC
    You could use the DateCalc function provided with Date::Manip. I have constructed a little code fragment to illustrate the usage:
    use Date::Manip; $weekno = 30; # want the 30th week of the year; $day = &DateCalc("1/1/2003","+${weekno}week"); $daydiff = 7 - &UnixDate($day, "%w"); $day1 = &DateCalc($day, "+${daydiff}day"); $day2 = &DateCalc($day1, "+6day"); print "$day1, $day2\n";
Re: Get date interval from week of year -> DateTime
by fglock (Vicar) on Sep 08, 2003 at 13:40 UTC

    Yet another way to do it...

    use DateTime::Event::Recurrence; my $now = DateTime->now; my $weeks = DateTime::Event::Recurrence->weekly; print $weeks->current( $now )->ymd,"\n"; print $weeks->next( $now )->ymd,"\n"; # output 2003-09-08 2003-09-15

    update: s/previous/current/

Re: Get date interval from week of year -> Date::Manip
by LupoX (Pilgrim) on Sep 08, 2003 at 08:03 UTC

    Thank you for the unbelievable fast help!

    Georg

Re: Get date interval from week of year -> Date::Manip
by Anonymous Monk on Sep 08, 2003 at 07:40 UTC
    use DateTime; my $now = DateTime->now; my $week = $now->week_number; print $now->ymd," now (week $week)\n"; my $first = DateTime->new( year => $now->week_year, month => 1, day => 1, ); print $first->ymd," beginning of the year \n";; $first->add( weeks => $now->week_number ); print $first->ymd, " beginning of week $week\n"; $first->add( weeks => 1 ); print $first->ymd, " end of week $week\n"; __END__ 2003-09-08 now (week 37) 2003-01-01 beginning of the year 2003-09-17 beginning of week 37 2003-09-24 end of week 37
      I'm glad to see others using DateTime, but that's not quite right. January 1st is not neccessarily the beginning of the week, I modified your snippet like so update: aren't I funny {grin} every year has 52 weeks ($last->week_number, lol).

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

      2003-09-08 now (week 37) 2003-01-01 beginning of the year 2003-09-17 beginning of week 37 2003-09-24 end of week 37

      Well, it's nice that people test their code before posting it, but if you don't look at the output then what's the point? According to your program, Sep 8 is between Sep 17 and Sep 24.

      Abigail

        How do you draw that conclusion? (nowhere do I say that Sept8 is between Sept17 and Sept24). It's like you're purposefuly being rude ;(

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://289695]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found