KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Finding the week of a specified date of past, present, or future
PRODUCT: 4D | VERSION: 15.x | PLATFORM: Mac & Win
Published On: May 19, 2017

Ever wanted to find out what was the week of a specified date that was how many weeks before, after, or at current? This utlity method can do the job:

// --------------------------------------------------------------------------------
// Name: DATE_OF_WEEK_OF_SUN_SAT
// Description: Method will determine the specified date and return the week
// before, after, or current of Sunday as the first day and Saturday at the last
// day which is the week of.
// Input:
// $1 (DATE) - Date to reference
// $2 (POINTER) - Pointer return the first day of Sunday date
// $3 (POINTER) - Pointer return the last day of Saturday date
// $4 (LONGINT) - Number of weeks of the date of reference for the week of
// --------------------------------------------------------------------------------
C_DATE($1;$date)
C_POINTER($2;$dateSun)
C_POINTER($3;$dateSat)
C_LONGINT($4;$weeksToCalculate)

If (Count parameters=4)
   
    $date:=$1
    $dateSun:=$2
    $dateSat:=$3
    $weeksToCalculate:=$4
   
    Case of
      :($weeksToCalculate<0)
        $dateSun->:=$date-(7*$weeksToCalculate*(-1))-(Day number($date)-1)
   
      : ($weeksToCalculate>0)
        $dateSun->:=$date+(7*$weeksToCalculate)-(Day number($date)-1)
   
    Else
        $dateSun->:=$date-(Day number($date)-1)
    End case
   
    $dateSat->:=$dateSun->+6
   
End if



Here are some examples of using the method DATE_OF_WEEK_OF_SUN_SAT:

1. Finding the current week of 5/24/1984:

C_DATE($dateSun;$dateSat)

DATE_OF_WEEK_OF_SUN_SAT (!5/24/1984!;->$dateSun;->$dateSat;0) // $dateSun - 5/20/84
//$dateSat - 5/26/84


2. Finding 2 weeks before of 5/24/1984:

DATE_OF_WEEK_OF_SUN_SAT (!5/24/1984!;->$dateSun;->$dateSat;-2) // $dateSun - 5/6/84
//$dateSat - 5/12/84


3. Finding 2 weeks after of 5/24/1984:

DATE_OF_WEEK_OF_SUN_SAT (!5/24/1984!;->$dateSun;->$dateSat;2) // $dateSun - 6/3/84
//$dateSat - 6/9/84