INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Usefull Functions & Procedures

ISOWEEK(): Calculate the correct ISO week with this UDF. by mmerlinn
Posted: 12 Jul 06 (Edited 18 Jul 06)

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.


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)


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


?ISOWEEK()
28
        (ISO week number for today July 12, 2006)

?ISOWEEK({12/31/2009})
53


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


?ISOWK1()
01/02/2006
        (Beginning date for the ISO year 2006)

?ISOWK1(2009)
12/29/2008


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


?ISOWKSTART()
07/10/2006
        (Beginning date for the ISO week for today July 12, 2006)

?ISOWKSTART({12/31/2009})
12/28/2009


smile

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close