|
|
|
| 11 June 2002 | WeekDaysDiff() |
| Author | W.M. Hinsch (New Mexico Mark) |
| Action | Calculate the number of week days (M-F) between two dates. |
| Syntax | WeekDaysDiff (Date, [DateOrDays]) |
| Parameters | |
| Remarks | I was just looking for an easy way to only count M-F weekdays between two dates. The idea came about because of the way our help desk system included weekend days in date calculations. It was frustrating to the help desk folks to start a problem Friday, finish it Monday, and have it counted as three days to repair. I developed the algorithm in KiXtart with help from the folks at "http://www.kixtart.org". |
| Returns | Integer number of week days (M-F) between two dates. |
| Dependencies | KiXtart 4.1+, Flip_SerDate() (SerialDate may be used as well, but the code and examples will have to be appropriately modified) |
| Examples | FOR $i = 1 TO 8 $tDt=Flip_SerDate(@DATE) $sDt=Flip_SerDate($tDt) $eDt=Flip_SerDate($tDt+$i) 'Elapsed week days $sDt to $eDt: ' + WeekDaysDiff($sDt,$eDt) ? 'Elapsed week days $sDt + $i: ' + WeekDaysDiff($sDt,$i) ? $eDt=Flip_SerDate($tDt-$i) 'Elapsed week days $sDt to $eDt: ' + WeekDaysDiff($sDt,$eDt) ? 'Elapsed week days $sDt - $i: ' + WeekDaysDiff($sDt,-$i) ? ? NEXT ; Returns at console ;Elapsed week days 2002/06/11 to 2002/06/12: 1 ;Elapsed week days 2002/06/11 + 1: 1 ;Elapsed week days 2002/06/11 to 2002/06/10: 1 ;Elapsed week days 2002/06/11 - 1: 1 ;Elapsed week days 2002/06/11 to 2002/06/13: 2 ;Elapsed week days 2002/06/11 + 2: 2 ;Elapsed week days 2002/06/11 to 2002/06/09: 1 ;Elapsed week days 2002/06/11 - 2: 1 ;Elapsed week days 2002/06/11 to 2002/06/14: 3 ;Elapsed week days 2002/06/11 + 3: 3 ;Elapsed week days 2002/06/11 to 2002/06/08: 1 ;Elapsed week days 2002/06/11 - 3: 1 ;Elapsed week days 2002/06/11 to 2002/06/15: 3 ;Elapsed week days 2002/06/11 + 4: 3 ;Elapsed week days 2002/06/11 to 2002/06/07: 2 ;Elapsed week days 2002/06/11 - 4: 2 ;Elapsed week days 2002/06/11 to 2002/06/16: 3 ;Elapsed week days 2002/06/11 + 5: 3 ;Elapsed week days 2002/06/11 to 2002/06/06: 3 ;Elapsed week days 2002/06/11 - 5: 3 ;Elapsed week days 2002/06/11 to 2002/06/17: 4 ;Elapsed week days 2002/06/11 + 6: 4 ;Elapsed week days 2002/06/11 to 2002/06/05: 4 ;Elapsed week days 2002/06/11 - 6: 4 ;Elapsed week days 2002/06/11 to 2002/06/18: 5 ;Elapsed week days 2002/06/11 + 7: 5 ;Elapsed week days 2002/06/11 to 2002/06/04: 5 ;Elapsed week days 2002/06/11 - 7: 5 ;Elapsed week days 2002/06/11 to 2002/06/19: 6 ;Elapsed week days 2002/06/11 + 8: 6 ;Elapsed week days 2002/06/11 to 2002/06/03: 6 ;Elapsed week days 2002/06/11 - 8: 6 |
| Source |
FUNCTION WeekDaysDiff ($sdate, OPTIONAL $vdateordays)
; Returns the integer number of week days (M-F) between two dates
; $sDate is a date strings in the form YYYY/MM/DD. Default is today.
; $vDateOrDays is either a date string in the form YYYY/MM/DD or an integer
; of the number of days to count back (neg. int.) or forward from $sEndDte
; Dependencies: Flip_SerDate() or SerialDate() and KiXtart 4.1+
; Note: If a serial date function is added to KiXtart, the iDOW
; calculation may need to be adjusted to get the correct DOW.
DIM $idiff, $idate, $istep, $idow, $ictr, $iend
IF ($sdate = '')
$sdate=@date
ENDIF
$idate=flip_serdate($sdate)
IF InStr($vdateordays,'/')
$itmp=flip_serdate($vdateordays)
$idiff=$itmp-$idate
ELSE
$idiff=0+$vdateordays
ENDIF
$weekdaysdiff=Abs(($idiff/7)*5) ; Account for full weeks
$idiff=$idiff MOD 7 ; remainder of days
$idow=($idate MOD 7)+7 ; Returns correct DOW (plus 7) for the ref date
IF ($idiff < 0) ; Subtracting days
$istep=-1
$idow=$idow-1 ; Start at the previous day
ELSE
$istep=1
$idow=$idow+1 ; Start at the next day
ENDIF
$ictr=$idow
$iend=$idow+$idiff
WHILE ($ictr <> $iend)
; Eliminate the weekends
IF NOT ($ictr=0 OR $ictr=6 OR $ictr=7 OR $ictr=13 OR $ictr=14 OR $ictr=20 OR $ictr=21)
$weekdaysdiff=$weekdaysdiff+1
ENDIF
$ictr=$ictr+$istep
LOOP
IF ($weekdaysdiff < 0)
$weekdaysdiff=$weekdaysdiff*-1
ENDIF
ENDFUNCTION ; - WeekDaysDiff -
|
|
|
|