Sybase Business Intelligence Solutions - Database Management, Data Warehousing Software, Mobile Enterprise Applications and Messaging
Sybase Brand Color Bar
delete

Search for    in all of Sybase.com
view all search results right arrow
  blank
 
 
 
 
 
 
 
 
 
 

 
 
CLICK TO EXPAND NAVIGATION
CLICK TO EXPAND NAVIGATION
 
 
 
 
Support > Technical Documents > Document Types > Tips > Calculating the Week Number

Calculating the Week Number

This technnical tip explains how to calculate the week number of a specific date.
 
RSS Feed
 
 
 

By: Boris Gasin,
Vice President of Technology,
Dynamic Technology Group

Question: How do I calculate the week number of a specific date?

Answer: There are a few different date standards, and each can affect how the week number is calculated. Before attempting to calculate a week number, you should resolve the following questions:

What is the first day of the week?

  • Sunday
  • Monday
When does the first week start?
  • January 1st
  • First full week of the year
  • First week with four days in a new year
  • First day of the first week of the year
In the United States, it is accepted that the week begins on a Sunday. This convention has deep religious roots and goes back to the book of Genesis.

However, the prevailing international standard for date and time formats is ISO 8601. According to ISO 8601, Monday is considered to be the first day of the week, and the first week of the year is the first week that has four days in the new year. A complete standard may be downloaded from the ISO Web site at http://www.iso.ch/markete/8601.pdf. Other conventions may be based on specific business rules.

The functions below provide an example of the week number calculation for the ISO standard. The algorithm can be easily modified to meet the requirements of a different convention.

Because the standard Powerscript DayNumber() function represents the US standard, a more generic of_daynumber function is created.


//***************************************
// Method: of_daynumber
// Author: B. Gasin
// Date  : 11/10/99
//
// Arg  :
// Return: integer
//
// Desc  : Function returns number of the
// day in the week
//	    ISO    US	
//    Mon = 1	    2
//    Tue = 2	    3
//    Wed = 3	    4
//    Thu = 4	    5
//    Fri = 5	    6
//    Sat = 6	    7
//    Sun = 7	    1
//
//***************************************
// Modifications:
// Date     Author              Comments
//---------------------------------------
//
//*************************************** 
integer li_rc
li_rc = DayNumber( ad_date )
 
// Convert to ISO
IF of_get_mode() = ISO then 
// ISO is a constant
li_rc --
IF li_rc = 0 THEN li_rc = 7
END IF
 
RETURN li_rc
Because the algorithm for the week number calculation is nonlinear, it has proved to be a little tricky. Up to first three days in January may belong to a previous year. Some years may contain 52 weeks, while others may have 53.

To determine the week number, I went with the "brute force" approach. First I calculated a starting point—a Thursday either preceding or following the date in question. Next I counted the weeks backward until the beginning of the year, as shown below.

//***************************************
// Method: of_isoweeknumber
// Author: B. Gasin
// Date : 11/10/99
//
// Arg : ad_date
// Return: integer
//
// Desc : Determines the week number of 
//        a date according to ISO
//	  standard.
// ISO Rules:
// - Monday is a 1st day of the week
// - 1st week of the year is considered 
//    to be the first week with 4 days
//
//***************************************
// Modifications:
// Date Author Comments
//---------------------------------------
//
//***************************************

date ld_date
Integer li_year, li_prev_year
Integer li_day, li_week


ld_date = ad_date

li_day = of_DayNumber(ld_date)


DO WHILE li_day <> 4 
IF li_day < 4 THEN 
   // Mon - Wed. Go to next Thur.
   ld_date = RelativeDate(ld_date, 1)
ELSE
   // Thur - Sun. Go to prev Thur.
   ld_date = RelativeDate(ld_date, -1)
END IF
li_day = of_DayOfWeek(ld_date)
LOOP

li_year = Year(ld_date)


// Count weeks backwards until the
// previous year
DO
   li_week++
   ld_date = RelativeDate(ld_date, -7)
   li_prev_year = Year(ld_date)
LOOP UNTIL li_year <> li_prev_year

Return li_week

 

DOCUMENT ATTRIBUTES
Last Revised: Jan 26, 2000
Product: PowerBuilder
Technical Topics: Troubleshooting, Application Development
  
Business or Technical: Technical
Content Id: 1002382
Infotype: Tips
 
 
 

 
© Copyright 2014, Sybase Inc. - v 7.6 Home / Contact Us / Help / Jobs / Legal / Privacy / Code of Ethics