Date Manipulation: Week_Number back to Date

W

wls

I've found a number of functions and expressions for taking dates and
converting them to a week number.

Interestingly enough, I need to go in the reverse direction.

( $day, $mon, $year ) = ReverseTheProcess( 2007, 7 ); # Week 7 of 2007
is 4-Feb-2007

Is there such a beast, or does someone have a clever way to calculate
it?

Thanks,
(e-mail address removed)
 
M

Martien Verbruggen

I've found a number of functions and expressions for taking dates and
converting them to a week number.

Interestingly enough, I need to go in the reverse direction.

( $day, $mon, $year ) = ReverseTheProcess( 2007, 7 ); # Week 7 of 2007
is 4-Feb-2007

Week 7 of 2007 is a range of seven days. 4 Feb 2007 is a Sunday, not a
week.
Is there such a beast, or does someone have a clever way to calculate
it?

The Date::Calc module has a method

Monday_of_Week
($year,$month,$day) = Monday_of_Week($week,$year);

which probably is what you want. If you need the Sunday instead of the
Monday, also use Add_Delta_Days() or one of those.

http://search.cpan.org/

Martien
 
G

Gunnar Hjalmarsson

The Date::Calc module has a method

Monday_of_Week
($year,$month,$day) = Monday_of_Week($week,$year);

That's probably the best way. Still, out of curiosity I gave it a try
using my favorite module Time::Local. ;-)

sub MondayOfWeek {
my ($year, $week) = @_;
require Time::Local;
import Time::Local 'timegm';
my $firstday = ( gmtime timegm(0,0,0,1,0,$year-1900) )[6];
my ($d, $m, $y) = ( gmtime( ($week-1)*604800 +
timegm(0,0,0,(1+(8-$firstday)%7),0,$year-1900) ) )[3..5];
sprintf '%d-%02d-%02d', $y+1900, $m+1, $d
}

print MondayOfWeek( 2007, 7 ), "\n";
 
B

brian d foy

Gunnar Hjalmarsson said:
Martien Verbruggen wrote:

That's probably the best way. Still, out of curiosity I gave it a try
using my favorite module Time::Local. ;-)

sub MondayOfWeek {


This sort of thing makes me think there should be a big
book of Date Recipes in <Foo>, just like we have Numerical
Recipes in <Foo>. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top