Return to the RPG Tips
How to Determine the Week of the Year for a Given Date
You're probably familiar with applications that need to know on what
day of the week a given date falls and with programming techniques
that derive this information. Some applications must also determine in
which week of the year a given date falls.
Believe it or not, there is an International Standards Organization
(ISO) standard that defines the rules for the week of the year! ISO
8601 states that Monday marks the beginning of the week and that the
first week of a year is the week that contains the first Thursday of
the year. This also means that the first week of the year is the week
that contains January 4.
The following service program, Dates, implements the ISO 8601 standard
using procedures RtvDayOfWeek and RtvWeekOfYear. Each of these
procedures accepts a single input parameter containing the date for
which the procedure is to retrieve information. Procedure RtvDayOfWeek
returns an integer in the range of 0 through 6 where the numbers
indicate the days of the week -- 0 indicates Monday, 1 indicates
Tuesday, and so on. Procedure RtvWeekOfYear returns an integer in the
range of 1 through 53, which indicates the week of the year in which
the specified date occurs.
Procedure RtvDayOfWeek is quite straightforward. It simply defines a
known Monday date, 2001-05-21, and then uses modulus arithmetic to
determine the day of the week for a specified date.
Procedure RtvWeekOfYear first calculates the date of the first Monday
in the target year. Next, if the target date is prior to the first
Monday of the target year, the program decrements the year (because
the target date occurs in a week that is actually in the prior year)
and re-calculates the first Monday. The program then uses modulus
arithmetic to calculate and return the week of the year in which the
target date occurs. Remember that though a date may occur in one
calendar year (i.e., dates at the beginning of January), because of
ISO 8601 it may actually be in a week from the prior year.
Below is service program Dates:
* ================================================================== * = Service program... Dates = * = Description....... Date routines = * = = * = CrtRPGMod Module( Dates ) = * = CrtSrvPgm SrvPgm( Dates ) Module( Dates ) Export( *All ) = * ================================================================== H NoMain * ================================================================== * = Prototypes = * ================================================================== * ------------------------------------------------------------------ * - RtvDayOfWeek Retrieve day of week - * - - * - Parameter Usage Type - * - Date Input Date field - * - Return data - * - Day of week Return Integer (0=Monday, 1=Tuesday, etc.)- * ------------------------------------------------------------------ D RtvDayOfWeek PR 5I 0 D D Value * ------------------------------------------------------------------ * - RtvWeekOfYear Retrieve week of year - * - - * - Parameter Usage Type - * - Date Input Date field - * - Return data - * - Week of year Return Integer - * ------------------------------------------------------------------ D RtvWeekOfYear PR 5I 0 D D Value * ================================================================== * = Procedure..... RtvDayOfWeek = * = Description... Retrieve day of week = * ================================================================== P RtvDayOfWeek B Export D RtvDayOfWeek PI 5I 0 D DateIn D Value * ------------------------------------------------------------------ * - Data definitions - * ------------------------------------------------------------------ D BaseMonday S D Inz( D'2001-05-21' ) D NbrDays S 10I 0 * ------------------------------------------------------------------ * - Calculate and return day of week - * ------------------------------------------------------------------ C DateIn SubDur BaseMonday NbrDays : *D C Return ( %Rem( %Rem( NbrDays : 7 ) + 7 : 7 ) ) P RtvDayOfWeek E * ================================================================== * = Procedure..... RtvWeekOfYear = * = Description... Retrieve week of year = * ================================================================== P RtvWeekOfYear B Export D RtvWeekOfYear PI 5I 0 D DateIn D Value * ------------------------------------------------------------------ * - Data definitions - * ------------------------------------------------------------------ D DS D Jan4Date D Inz( D'0001-01-04' ) D Jan4Year 4S 0 Overlay( Jan4Date ) D MondayDate S D D Jan4Day S 5I 0 D NbrOfDays S 10I 0 D SundayDate S D D ISOWeek S 5I 0 * ------------------------------------------------------------------ * - Calculate date of the last day of the week (Sunday) for the - * - target week and calculate the week number for that Sunday to - * - derive the week number for the target date - * ------------------------------------------------------------------ C Eval NbrOfDays = 6 - RtvDayOfWeek( DateIn ) C DateIn AddDur NbrOfDays:*D SundayDate * ------------------------------------------------------------------ * - Set date to January 4 of the calculated year and use to derive - * - the date of that year's first Monday - * ------------------------------------------------------------------ C Extrct SundayDate:*Y Jan4Year C Eval Jan4Day = RtvDayOfWeek( Jan4Date ) C Jan4Date SubDur Jan4Day:*D MondayDate * ------------------------------------------------------------------ * - Calculate the week of the year and return the value - * ------------------------------------------------------------------ C SundayDate SubDur MondayDate NbrOfDays:*D C If NbrOfDays < 0 C Eval Jan4Year = Jan4Year - 1 C Eval Jan4Day = RtvDayOfWeek( Jan4Date ) C Jan4Date SubDur Jan4Day:*D MondayDate C SundayDate SubDur MondayDate NbrOfDays:*D C EndIf C Eval ISOWeek = NbrOfDays/7 C If ISOWeek >= *Zero C Eval ISOWeek = ISOWeek + 1 C Else C Eval ISOWeek = 53 C EndIf C Return ISOWeek P RtvWeekOfYear EIf your business calendar dictates a different standard for the day of the week on which a week begins or defines the week of the year using different rules, you can use service program Dates as a model for writing a program that implements your business rules.
[report a broken link by clicking here]