[Date-Calc] Extract each 1st (or 2,3..) Monday of month ?

A

Alextophi

hello,
I do not know how to make to extract each 1st (or 2,3..) Monday (or
Tuesday, Wednesday..)
from each month over one 2 months period (for example)

ex: I want to know the exact date from the every 1st Sunday of the
month.


*------------------------------------------------------------------------*
for ( $i = 0; $i <= 60; $i++ ) { # Buckle on 60 iterations (days),

@date = Add_Delta_Days(@today,$i); # Incrementing +1,
if (Day_of_Week(@date) == $searching_dow) { # if the day of
week is "scale at the recherch day",
printf("%4d/%02d/%02d\n", @date); # post the every day of
week (ex; every Sunday),
}
}
*------------------------------------------------------------------------*


- the problem is that I obtains every Sunday and not 1st Sunday



TANK'S
christophe(FR)
 
S

samsee

Hello,
Use break

for ( $i = 0; $i <= 60; $i++ ) {
@date = Add_Delta_Days(@today,$i);
if (Day_of_Week(@date) == $searching_dow) {
printf("%4d/%02d/%02d\n", @date);
break; // if 1st someday(Mon, Tue...) is catched, then end to
loop
} // End If
} // End for

Having Fun. good bye
 
I

it_says_BALLS_on_your_forehead

samsee said:
Hello,
Use break

for ( $i = 0; $i <= 60; $i++ ) {
@date = Add_Delta_Days(@today,$i);
if (Day_of_Week(@date) == $searching_dow) {
printf("%4d/%02d/%02d\n", @date);
break; // if 1st someday(Mon, Tue...) is catched, then end to
^^^ 'last' ^^^^^^^^^ 'caught'
 
J

Josef Moellers

Alextophi said:
hello,
I do not know how to make to extract each 1st (or 2,3..) Monday (or
Tuesday, Wednesday..)
from each month over one 2 months period (for example)

ex: I want to know the exact date from the every 1st Sunday of the
month.

You can use timelocal() from Time::Local to convert noon of the 1st of a
month to its time value, then use localtime to find out what weekday
that is. The, from the weekday, infer how may days till next sunday (use
an array for that):

use warnings;
use strict;
use Time::Local;

my @firstsun = (
1, 7, 6, 5, 4, 3, 2
);
my ($m, $y) = (2, 2006);
my $wd = (localtime(timelocal(0, 0, 12, 1, $m-1, $y-1900)))[6];
# This would return 3, as Feb, 1 2006 was a Wednesday
print "The first sunday was/is/willbe on the ", $firstsun[$wd], "\n";

HTH,

Josef
 
U

Uri Guttman

s> Hello,
s> Use break

s> for ( $i = 0; $i <= 60; $i++ ) {
s> @date = Add_Delta_Days(@today,$i);
s> if (Day_of_Week(@date) == $searching_dow) {
s> printf("%4d/%02d/%02d\n", @date);
s> break; // if 1st someday(Mon, Tue...) is catched, then end to
s> loop
s> } // End If
s> } // End for

what language is that? perl has no break command nor does it support //
comments.

uri
 
S

Samwyse

Josef said:
You can use timelocal() from Time::Local to convert noon of the 1st of a
month to its time value, then use localtime to find out what weekday
that is. The, from the weekday, infer how may days till next sunday (use
an array for that):

One problem that has tripped me up when working on things like this
involves crossing over the start and end of daylight savings time. Make
sure that you test for dates in April and October.
 
U

usenet

Alextophi said:
I do not know how to make to extract each 1st (or 2,3..) Monday (or Tuesday,
Wednesday..) from each month over one 2 months period (for example)

You don't say what format you want the dates in. Of course, you can
have any format you want by changing the UnixDate format string...

#!/usr/bin/perl
use strict; use warnings;

use Date::Manip;

my $start = '01/01/2006';
my $end = '12/31/2006';

my @mondays_in_2006= ParseRecur("0:0:1*1:0:0:0", $start,$start,$end,);
my @first_mondays = ParseRecur("0:1*1:1:0:0:0", $start,$start,$end,);
my @second_tuesdays= ParseRecur("0:1*2:2:0:0:0", $start,$start,$end,);
my @third_fridays = ParseRecur("0:1*3:5:0:0:0", $start,$start,$end,);

print map {UnixDate($_, "%Y-%m-%d") . "\n"} @third_fridays; #wow!

__END__
 
J

Josef Moellers

Samwyse said:
One problem that has tripped me up when working on things like this
involves crossing over the start and end of daylight savings time. Make
sure that you test for dates in April and October.

That's why I used noon. The chances are slim that DST changes during the
day ;-)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top