Date Format, howto calculate WEEK number (iso-8601

# Thread: Date Format, howto calculate WEEK number (iso-8601

1. Senior Member
Join Date
Dec 1969
Posts
147

## Date Format, howto calculate WEEK number (iso-8601

I need to calculate the week number (not the day in the week), based on a given date. This must be done in ASP (not VB or C++).<BR><BR>Examples:<BR>January 3, 1999 equals Day 7 in week 53 (1998)<BR>January 4, 1999 equals Day 1 in week 1 (1999)<BR>January 3, 2000 equals Day 1 in week 1 (2000)<BR><BR>January 1&#039st is not always a Monday, but Mondays are always the first day of the week.<BR><BR>And no, WeekDay() does not do this.<BR><BR>Any suggestions?<BR>

2. jd
Senior Member
Join Date
Dec 1969
Posts
850

## RE: Date Format, howto calculate WEEK number (iso-

Sheesh. Thimk. I already answered this.<BR><BR>Your first post asked how to come up with a weekday from 1 to 7, which weekday DOES do. Now you are asking a slightly different question, but my previous post still applies.<BR><BR>With the DateDiff function, you can calculate a week number for any date for a given year (based on using a Jaunary 1 date as the start). You can also calculate differences in days based on a start and an end. Using relatively simple math you can turn a day difference into weeks. <BR><BR>With the DATEADD function, you can easily perform the opposite calculation.<BR><BR>Would you like me to write this for you?

3. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Yes please

i never thought of it that way and still dont quite konw what you are saying...write it please JD

4. jd
Senior Member
Join Date
Dec 1969
Posts
850

## RE: Yes please

It&#039s pretty straightforward. The ISO standard states that week 1 is the week that contains the first Thursday of the year. Odd, perhaps, but that&#039s what happens when committees work on things. I&#039ll whip something up.<BR><BR>Give me a few minutes.<BR>

5. Senior Member
Join Date
Dec 1969
Posts
147

## RE: Date Format, howto calculate WEEK number (iso-

If you can write this - you are a star!<BR><BR>My first post asked about weeknumber AND weekday, not just weekday. Here it is again:<BR>----------------------------<BR>I need to convert a date into the week number (1-52) and week day (1-7) but cannot find any functionality in ASP for this. <BR><BR>I also need to go the other way, i.e. converting Year, Week Number and Week Day into a date. <BR>----------------------------

6. jd
Senior Member
Join Date
Dec 1969
Posts
850

## RE: Date Format, howto calculate WEEK number (iso-

Sorry this is quick and dirty, but I think (hope?) it will help you.<BR><BR>&#060;%<BR><BR>sub GetISOWeekDay(dtDate, intWeek, intDay, intYear)<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp; dim intYY, intMM, intDD<BR>&nbsp;&nbsp;&nbsp;&nbsp; dim intA<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp; intYY = Year(dtDate)<BR>&nbsp;&nbsp;&nbsp;&nbsp; intMM = Month(dtDate)<BR>&nbsp;&nbsp;&nbsp;&nbsp; intDD = Day(dtDate)<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp; intWeek = datediff("ww", cdate("1/1/" & intYY), dtDate, vbMonday, vbFirstFourDays)<BR>&nbsp;&nbsp;&nbsp;&nbsp; if intWeek = 0 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; intWeek = 53<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp; intYear = intYY - 1<BR>&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; intYear = intYY<BR>&nbsp;&nbsp;&nbsp;&nbsp; end if<BR> <BR>&nbsp;&nbsp;&nbsp;&nbsp; intDay = weekday(dtDate, vbMonday) <BR><BR>end sub<BR><BR><BR><BR>&#039 Test code for above routine<BR><BR>dim intW, intD, intY<BR><BR>GetISOWeekDay cdate("1/3/2000"), intW, intD, intY<BR><BR>response.write "ISO: Week=" & intW & " Year=" & intY & " Day=" & intD<BR><BR><BR>%&#062;

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not date format...cannot be done that way...

Hmmm...this one looks like fun!<BR><BR>Okay, let&#039s figure out the algorithm to use:<BR><BR>(1) Start with Jan 1 of the year you are interested in.<BR>What day is it on?<BR><BR>(2) If not Monday, then find next subsequent Monday. Use this as day1 of your "ISO" year.<BR><BR>(3) Count days between "day1" and the date you have. Divide by 7. That gives you week number.<BR><BR>(4) If the date you are interested in is *before* "day1" (you will get a negative number of days, then it belongs in prior year, so start all over again with last year&#039s Jan 1 date.<BR><BR>Now, to write the code.......<BR><BR>Here&#039s a function *AND* some "wrapper" code that tests it. I checked Jan 1 through Jan 8 in all years, 1992 to 2000. It got every one of them right.<BR><BR>Oh, and Jan 3, 1999 is Day 7 in week 52 of 1998, *NOT* week 53. Your assertion it was week 53 caused me to have to check it carefully! You are wrong. <BR><BR>Notice how you must call the function passing *both* the date *and* the year of that date. That&#039s so I can recursively call it when I discover that your date actually falls in last week of prior year. We could fix that by having two functions, but this works for me.<BR><BR>********************<BR><BR>&LT;%<BR>Fu nction ISOWeekNumber( dt, yr )<BR>&nbsp;&nbsp;&nbsp;&nbsp;jan1 = DateSerial( yr, 1, 1 ) <BR>&nbsp;&nbsp;&nbsp;&nbsp;wkdy = WeekDay( jan1 )<BR>&nbsp;&nbsp;&nbsp;&nbsp;If wkdy = 2 Then &#039 if Jan1 is on a Monday...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;day1 = jan1 &#039 ready to go<BR>&nbsp;&nbsp;&nbsp;&nbsp;ElseIf wkdy = 1 Then &#039 Jan1 is on Sunday<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;day1 = DateSerial( yr, 1, 2 ) &#039 so first day is Jan 2, on Monday<BR>&nbsp;&nbsp;&nbsp;&nbsp;Else &#039 for all other days, use following Monday:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;day1 = DateSerial( yr, 1, 10 - WeekDay( jan1 ) )<BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;dayCount = DateDiff( "d", day1, dt )<BR>&nbsp;&nbsp;&nbsp;&nbsp;If dayCount &LT; 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;temp = ISOWeekNumber( dt, yr-1 ) &#039 use last year, instead<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;ISOWeekNumber = temp<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Exit Function<BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;ISOWeekNumber = 1 + (dayCount 7) &#039 integer division, toss remainder<BR>End Function<BR>%&#062;<BR><BR>&LT;HTML&#062;&LT;BODY& #062;<BR><BR>&LT;%<BR>For yr = 1992 TO 2000<BR>&nbsp;&nbsp;&nbsp;&nbsp;For dy = 1 To 8<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;dt = DateSerial( yr, 1, dy )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;Response.Write "ISOWeekNumber for " & dt & " is " _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ; & ISOWeekNumber( dt,Year(dt) ) _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ; & ", weekday is " & WeekDay(dt,2) & "&LT;BR&#062;" & vbNewLine<BR>&nbsp;&nbsp;&nbsp;&nbsp;Next<BR>Next< BR>%&#062;<BR><BR>&LT;/BODY&#062;&LT;/HTML&#062;<BR><BR>

8. jd
Senior Member
Join Date
Dec 1969
Posts
850

## RE: Not date format...cannot be done that way...

:)

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## Hmmm????

He said Monday was first day of the ISO week. You say Thursday. I took him at his word.<BR><BR>But why is the week always 53 if it belongs to prior year???<BR><BR>My calcs say "it depends". Sometimes, it is only week 52 of prior year. Of is this another ISO-ism?<BR><BR>

10. jd
Senior Member
Join Date
Dec 1969
Posts
850

## RE: Hmmm????

No, I don&#039t say that Thursday is the first day of the week, I said that week 1 of any ISO year is the first week of the CALENDAR year that CONTAINS the Thursday.<BR><BR>Day 1 of the week is Monday.<BR><BR>:)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•