
Date Format, howto calculate WEEK number (iso8601
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'st 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>

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?

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

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

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 (152) and week day (17) 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>

RE: Date Format, howto calculate WEEK number (iso
Sorry this is quick and dirty, but I think (hope?) it will help you.<BR><BR><%<BR><BR>sub GetISOWeekDay(dtDate, intWeek, intDay, intYear)<BR><BR> dim intYY, intMM, intDD<BR> dim intA<BR><BR> intYY = Year(dtDate)<BR> intMM = Month(dtDate)<BR> intDD = Day(dtDate)<BR><BR> intWeek = datediff("ww", cdate("1/1/" & intYY), dtDate, vbMonday, vbFirstFourDays)<BR> if intWeek = 0 then<BR> intWeek = 53<BR> &n bsp; intYear = intYY  1<BR> else<BR> intYear = intYY<BR> end if<BR> <BR> intDay = weekday(dtDate, vbMonday) <BR><BR>end sub<BR><BR><BR><BR>' 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>%>

Not date format...cannot be done that way...
Hmmm...this one looks like fun!<BR><BR>Okay, let's 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's Jan 1 date.<BR><BR>Now, to write the code.......<BR><BR>Here's 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's 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><%<BR>Fu nction ISOWeekNumber( dt, yr )<BR> jan1 = DateSerial( yr, 1, 1 ) <BR> wkdy = WeekDay( jan1 )<BR> If wkdy = 2 Then ' if Jan1 is on a Monday...<BR> & nbsp; day1 = jan1 ' ready to go<BR> ElseIf wkdy = 1 Then ' Jan1 is on Sunday<BR> &nbs p; day1 = DateSerial( yr, 1, 2 ) ' so first day is Jan 2, on Monday<BR> Else ' for all other days, use following Monday:<BR> &nb sp; day1 = DateSerial( yr, 1, 10  WeekDay( jan1 ) )<BR> End If<BR> dayCount = DateDiff( "d", day1, dt )<BR> If dayCount < 0 Then<BR> temp = ISOWeekNumber( dt, yr1 ) ' use last year, instead<BR> &nb sp; ISOWeekNumber = temp<BR> Exit Function<BR> End If<BR> ISOWeekNumber = 1 + (dayCount 7) ' integer division, toss remainder<BR>End Function<BR>%><BR><BR><HTML><BODY& #062;<BR><BR><%<BR>For yr = 1992 TO 2000<BR> For dy = 1 To 8<BR> &nb sp;dt = DateSerial( yr, 1, dy )<BR> &nb sp;Response.Write "ISOWeekNumber for " & dt & " is " _<BR> &nb sp;   ; & ISOWeekNumber( dt,Year(dt) ) _<BR> &nb sp;   ; & ", weekday is " & WeekDay(dt,2) & "<BR>" & vbNewLine<BR> Next<BR>Next< BR>%><BR><BR></BODY></HTML><BR><BR>

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

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 ISOism?<BR><BR>

RE: Hmmm????
No, I don't 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

Forum Rules

