× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Here it is an interesting article. Sincerely Domenico 

 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 =

* ==================================================================

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, ...) -

* ------------------------------------------------------------------

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

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 =

* ==================================================================

RtvWeekOfYear B Export

D RtvWeekOfYear PI 5I 0

D DateIn D Value

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

* ------------------------------------------------------------------

* - Set date to January 4 of target year and use to calculate the -

* - date of the first Monday of the target year -

* ------------------------------------------------------------------

C Extrct DateIn:*Y Jan4Year

C Eval Jan4Day = RtvDayOfWeek( Jan4Date )

C Jan4Date SubDur Jan4Day:*D MondayDate

* ------------------------------------------------------------------

* - If the target date is prior to the first Monday of the target -

* - year, adjust the year to reflect the prior year and calculate -

* - the date of the first Monday of the year again -

* ------------------------------------------------------------------

C If DateIn < MondayDate

C Eval Jan4Year = Jan4Year - 1

C Eval Jan4Day = RtvDayOfWeek( Jan4Date )

C Jan4Date SubDur Jan4Day:*D MondayDate

C EndIf

* ------------------------------------------------------------------

* - Calculate the week of the year and return the value -

* ------------------------------------------------------------------

C DateIn SubDur MondayDate NbrOfDays:*D

C Return ( NbrOfDays / 7 ) + 1

P RtvWeekOfYear E

 

It's easy to use procedure RtvWeekOfYear in your applications. Below

is a code fragment that demonstrates its use:

* ==================================================================

* = Sample snippet using procedure RtvWeekOfYear =

* ==================================================================

D RtvWeekOfYear PR 5I 0

D D Value

D MyDate S D Inz( D'2001-05-24' )

D WeekOfYear S 5I 0

C Eval WeekOfYear = RtvWeekOfYear( MyDate )

 

If 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.


As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.