function BusinessDaysBetweenDates(d1, d2)

You'll find the code below...

I've written in "pseudo-code" but fairly close to JavaScript, as I'm going
to port it to JavaScript shortly. It should explain how this function works.
CAREFUL - It's NOT yet been tested!!
I have previously ported significant portions of the code from the book
"Standard C Date/Time Library" (Lance Latham, R&D Books, 1998) to
JavaScript already and the code I, myself, ultimately want will
be comparable with with this library. The C-code is copyright R&D books,
but my JS port is not so protected, so let me know if you want the rest of
the library as well.
I use this library because it was always such a pain to perform date
calculations in Witango, so I always perform them in JavaScript. And then I
also have the same date functions available for any client-side work.

Enjoy!

Anthony -


/* Copyright 2012 Anthony M. Humphreys <[email protected]>
// Permission granted to use and/or modify this code in any way
//
// businessDatesBetweenDates(d1, d2)
// returns the number of business days between two dates
//
// inputs:
// d1 = int, number of days from the epoch
// d2 = int, number of days from the epoch
//
// assumptions:
// modulus(d1,7) == d1 % 7 == 0 == Sunday
// modulus(d1,7) == d1 % 7 == 1 == Monday
// modulus(d1,7) == d1 % 7 == 2 == Tuesday, etc
// modulus(d1,7) == d1 % 7 == 6 == Saturday
//
*/

function businessDatesBetweenDates (d1, d2) {

// assign smaller day to StartDay
if (d1 < d2) {
startDay = d1;
EndDay = d2;
} else {
startDay = d2;
EndDay = d1;
}

// correct for startDay on Weekend by
// moving startDay to following Monday
if ((StartDay % 7) == 6) { // (StartDay == Sat)
StartDay = StartDay + 2
} else if ((StartDay % 7) == 0) { // (StartDay = Sun)
StartDay = StartDay +1
}

// correct for EndDay on weekend by
// moving endDay to previous Friday
if ((endDay % 7) == 0) { // (endDay = Sun)
endDay = EndDay - 2
} else if ((EndDay % 7) == 6) { // (EndDay = Sat)
EndDay = EndDay - 2
}

// Get WeekNumbers for StartDay and EndDay
StartWeek = int(StartDay / 7);
EndWeek   = int(EndDay / 7);

//calc Difference in weeks
wks = EndWeek - StartWeek

// different means of doing calculations
// depending on the difference
// in the number of weeks
if (wks == 0) { // Dates in same week
BusinessDays = Enday - StartDay + 1
// +1 because business days are _inclusive_
} else if (wks == 1) {
BusinessDays = (6 - (StartDay % 7)) + (EndDay % 7)
// number of business days left in Start week
// plus number of business days in End week
} else {
BusinessDays = (6 - (StartDay % 7)) + (EndDay % 7) + (5 * (wks -1))
// number of business days left in Start week
// plus number of business days in End week
// plus 5 days for every week inbetween
}
 // Done, return BusinessDays
return (BusinessDays);
}



On Mon, Jul 16, 2012 at 8:00 AM, WebDude <[email protected]> wrote:

> **
> Actually, I am looking for something like this...
>
>  BusinessTimeBetweenDates(D1,D2)
>
> *John Muldoon*
> Corporate Incentives
> 3416 Nicollet Ave S
> Minneapolis, MN 55408-4552
> 612.822.2222
> [email protected]
>  http://cipromo.com
>
>
>
>  ------------------------------
> *From:* Anthony Humphreys [mailto:[email protected]]
> *Sent:* Saturday, July 14, 2012 2:18 PM
> *To:* [email protected]
> *Subject:* Re: TeraScript-Talk: date calculations
>
> WebDude,
>
> Are you looking for a function like this?
> BusinessDaysBetweenDates(D1,D2)
>
>
>
> On Sat, Jul 14, 2012 at 2:24 PM, WebDude <[email protected]> wrote:
>
>> **
>> I have 2 timestamps in the db, we can call them starttime and endtime for
>> now. Has anyone ever wrote a down and dirty calculation that doesn't
>> include weekend days (i.e saturady and sunday? Holidays? I would settle for
>> just a weekend day omit for now. I have been farting around with this and
>> ended up with a bunch of code, but I was hoping for something more elegant.
>> In other words, I am trying to return the total time between the 2 dates
>> and the average time between the two dates for the total return. I started
>> by writing direct db actions to the db but it is pretty daunting for me.
>> Going from datetosecs is relatively easy to figuring this out. Trying to
>> omit Saturaday and Sunday gets a bit more complex, especially if you are
>> dealing with hundreds of returns.
>>
>> Witango 2000 for now... no Robert, I don't have the new servers up and
>> running yet. Hopefully soon!
>>
>>  ;-)
>>
>> WebDude
>> **
>>
>>
>> ------------------------------
>> To unsubscribe from this list, please send an email to
>> [email protected] with "unsubscribe terascript-talk" in the body.
>
>
>



----------------------------------------

To unsubscribe from this list, please send an email to [email protected] 
with "unsubscribe terascript-talk" in the body.

<<att28ad.gif>>

Reply via email to