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

Hi All, I'm working on a script that will accept two dates and return all business days within that range. I have it working however, when a weekend is within the date range, it restarts on the Monday date only. I don't know how to fix this and am hoping tht somebody here can assist. This is my code:
my $start_date = param('start_date'); my $end_date = param('end_date'); print header; print "$start_date to $end_date <br>"; my $start_date_parsed=ParseDate($start_date); my $end_date_parsed=ParseDate($end_date); print "$start_date_parsed to $end_date_parsed <br><br>"; my $delta=Delta_Format(DateCalc($start_date_parsed,$end_date_pa +rsed),'',"%dv"); print "$delta delta-days<br />\n"; for my $day (0..$delta) { my $this_date=DateCalc($start_date_parsed, "+$day day"); my $this_date_formatted=UnixDate($this_date,"%m-%d-%Y"); next unless ( Date_IsWorkDay($this_date)); print "For-Day $day: $this_date_formatted<br />"; } print end_html;
This is what is output from the date range 8/1/2014-8/9/2014:

08/04/2014 to 08/09/2014
2014080400:00:00 to 2014080900:00:00

5 delta-days
For-Day 0: 08-04-2014
For-Day 1: 08-05-2014
For-Day 2: 08-06-2014
For-Day 3: 08-07-2014
For-Day 4: 08-08-2014

This is what happens if I include a weekend in the range, 8/1/2014-8/15/2014:

08/04/2014 to 08/15/2014
2014080400:00:00 to 2014081500:00:00

4 delta-days
For-Day 0: 08-04-2014
For-Day 1: 08-05-2014
For-Day 2: 08-06-2014
For-Day 3: 08-07-2014
For-Day 4: 08-08-2014

This is what I'm actually looking for, with an example date range of 8/1/2014-8/15/2014:

08/04/2014 to 08/15/2014
2014080400:00:00 to 2014081500:00:00

9 delta-days
For-Day 0: 08-04-2014
For-Day 1: 08-05-2014
For-Day 2: 08-06-2014
For-Day 3: 08-07-2014
For-Day 4: 08-08-2014
For-Day 5: 08-11-2014
For-Day 6: 08-12-2014
For-Day 7: 08-13-2014
For-Day 8: 08-14-2014
For-Day 9: 08-15-2014

Replies are listed 'Best First'.
Re: Business Date Range Only
by poj (Abbot) on Aug 01, 2014 at 20:31 UTC
    I'm guessing you are not using the latest version of Date::Manip 6.46. You could try
    $delta=Delta_Format(DateCalc($start_date_parsed,$end_date_pa +rsed),'',"%dt"); # changed from %dv
    but if you did upgrade you could use the OO interface like this
    use strict; use Date::Manip; my $start_date = '8/4/2014'; my $end_date = '8/15/2014'; print "$start_date to $end_date \n"; my $date1 = new Date::Manip::Date; my $date2 = $date1->new_date(); my $err1 = $date1->parse($start_date); my $err2 = $date2->parse($end_date); while ($date1->cmp($date2) <= 0) { print $date1->printf("%m-%d-%Y")."\n"; $date1->next_business_day(1); }
    poj
      Excellent solution.
      I upgraded and this did exactly what I need it to do.
      Thanks a million!!
Re: Business Date Range Only
by GotToBTru (Prior) on Aug 01, 2014 at 17:47 UTC

    What modules are you using?

    1 Peter 4:10
      use Time::localtime; use Date::Calc qw(:all); use Date::Manip;

        When I run your code in debug, DateCalc($start_date_parsed,$end_date_parsed) returns an empty string. If I add a 3rd parameter, 2 (per an example I found), it returns the days between in the form '0:0:1:4:0:0:0'. I think you need to examine the requirements of the module routines more carefully.

        1 Peter 4:10