Skip to Content
Author's profile photo Sagar Girme

Custom Function to get Last business Date

Hi All,

Below function will give the last business date – Ranging from Monday to Friday.

1. On Tuesday- Last business date will be Monday’s date

2. On Monday – Last Business date will be – Friday’s date

3.On Saturday and Sunday – Last business date will be- Friday’s date.

Function:

Name: FN_GET_BUSINESS_DATE( )

Example : FN_GET_BUSINESS_DATE( -1, Date Value)

# Comments
# Parameters
Input
#   $P_I_START_DATE   date
#   $P_I_DAYS2ADD     int
#
Output
#   Return     int
#   $P_O_RETURN_DATE        date
##
# Variables
#   $L_RETURN_DATE   date
#           $L_COUNTER              Int
#           $L_CUR_DATE             date
#           $L_DAYNUM               INT
#           $L_SKIPCNTR             INT
#           $L_DIRECTION            INT
#           $L_BUSINESSDAYS         INT
#
# Example : FN_GET_BUSINESS_DATE( -1, Date Value)
#
# ———————————–

Code:

           $L_COUNTER = 0 ;
           $L_CUR_DATE = $P_I_START_DATE;
           $L_SKIPCNTR = 0;
           $L_DIRECTION = 1;
           $L_BUSINESSDAYS = $P_I_DAYS2ADD;

If ($P_I_DAYS2ADD  < 0)
begin
    $L_DIRECTION = – 1;
    $L_BUSINESSDAYS = (-1) * $L_BUSINESSDAYS;
end

while ( $L_COUNTER < $L_BUSINESSDAYS )
begin
  $L_CUR_DATE= $L_CUR_DATE + $L_DIRECTION;
  $L_DAYNUM = day_in_week($L_CUR_DATE);
         
      if($L_DAYNUM >=  1 AND $L_DAYNUM <= 5)
           begin
                   $L_COUNTER = $L_COUNTER +1;
                 end
            else
        begin
                   $L_SKIPCNTR = $L_SKIPCNTR +1;
                 end
end
$L_RETURN_DATE = $P_I_START_DATE+ ($L_DIRECTION * ($L_COUNTER + $L_SKIPCNTR));

Return $L_RETURN_DATE;

——————————————————–

Thanks & Regards,

Sagar Girme

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.