Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ISOWEEK(): Calculate the correct ISO week with this UDF.

Usefull Functions & Procedures

ISOWEEK(): Calculate the correct ISO week with this UDF.

by  mmerlinn  Posted    (Edited  )
[smile]

Recently in thread1551-1244574 a challenge was issued for code to determine the correct ISO week for any date. I thought about it for a while, then decided to see how easy that would be using FoxPro.

Since it is a worldwide standard, I figured that many of you need to be in compliance within your FoxPro code. I don't see me ever needing to determine ISO weeks, but I am sure many of you need a simple UDF like this in your arsenal of handy functions.

This link (http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm) has extensive information about ISO weeks. I did hundreds of tests using the 15 possible calendars as my benchmark. As of this time the following code seems to be error-free and working as it should.

There are three functions here. ISOWEEK() determines which ISO week (1 to 53) that a specific date falls in. ISOWK1() returns the actual starting date of an ISO year, always the Monday before the first Thursday of the common year. And ISOWKSTART() returns the actual date that the ISO week begins with, always the Monday beginning the ISO week of the date in question.

Note particularly that ISOWK1() does NOT return the starting date of an ISO year containing a specific date. Dates in any specific common year will fall into 1, 2, or 3 different ISO years. Most common years have dates that fall into 2 different ISO years. A few years have dates that all fall into only one ISO year, and a few have dates in 3 different ISO years.

These functions use American dates, however with minor changes they should work properly for any date configuration.

All three functions default to the current date that your computer is set to when no date is passed to the function. They will only work correctly if nothing is passed or a CORRECT date is passed. There is no error checking, so if you pass anything other than a date, you will get weird results.

[TT][COLOR BLUE]
FUNCTION ISOWEEK
PARAMETERS qDate
PRIVATE ALL LIKE z*

zdate = IIF(EMPTY(PARAMETERS()), DATE(), qDate)
zyr = YEAR(zdate)
zdays = zdate - ISOWK1(zyr)
zweek = INT(IIF(zdays < 0, zdate - ISOWK1(zyr - 1), zdays)/7)

RETURN IIF(zweek < 52, zweek, IIF(zyr = YEAR(ISOWK1(zyr + 1)), 0, 52)) + 1


FUNCTION ISOWK1
PARAMETERS qYear
PRIVATE ALL LIKE z*

zyear = IIF(EMPTY(PARAMETERS()), YEAR(DATE()), qYear)
zdate = CTOD('01/01/' + RIGHT('0000' + ALLTRIM(STR(zyear)), 4))

RETURN zdate - DOW(zdate) + IIF(DOW(zdate) < 6, 2, 9)


FUNCTION ISOWKSTART
PARAMETERS qDate

zdate = IIF(EMPTY(PARAMETERS()), DATE(), qDate)

RETURN zdate - DOW(zdate) + IIF(DOW(zdate) > 1, 2, -5)
[/COLOR][/TT]

Example #1 - Find the ISO week that a specific date falls within:

[TT]
?ISOWEEK()
[COLOR BLACK YELLOW]28[/COLOR]
(ISO week number for today July 12, 2006)

?ISOWEEK({12/31/2009})
[COLOR BLACK YELLOW]53[/COLOR]
[/TT]

Example #2 - Find the date that the ISO year begins with:

[TT]
?ISOWK1()
[COLOR BLACK YELLOW]01/02/2006[/COLOR]
(Beginning date for the ISO year 2006)

?ISOWK1(2009)
[COLOR BLACK YELLOW]12/29/2008[/COLOR]
[/TT]

Example #3 - Find the date that the ISO week begins with:

[TT]
?ISOWKSTART()
[COLOR BLACK YELLOW]07/10/2006[/COLOR]
(Beginning date for the ISO week for today July 12, 2006)

?ISOWKSTART({12/31/2009})
[COLOR BLACK YELLOW]12/28/2009[/COLOR]
[/TT]

[smile]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top