Getting the weeknumber

  • Thread starter Thread starter Morten Snedker
  • Start date Start date
M

Morten Snedker

In Denmark the 1/1/2009 belongs to week 53 of year 2008.

The code below, as I believe should apply to Danish settings, returns
1, which is wrong.

Am I having a wrong approach?


'--code begin
Dim iWeek As Integer
Dim gc As New System.Globalization.GregorianCalendar

gc.CalendarType = Globalization.GregorianCalendarTypes.Localized
iWeek = gc.GetWeekOfYear(#1/1/2009#,
Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

MsgBox(iWeek)
'-- code end --



I have a function for SQL-server that works perfectly, but is it
really necessary for such an ugly solution?

DECLARE @ISOweek varchar(10)
DECLARE @DATE datetime
DECLARE @s varchar(10)
DECLARE @Year int

SET @DATE='01-01-2009'
SET @ISOweek= CONVERT(varchar,DATEPART(wk,@DATE)+1
-DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104'))
--Special cases: Jan 1-3 may belong to the previous year
IF (@ISOweek='0')
SET
@ISOweek=CONVERT(varchar,dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1
AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS
CHAR(2)))+1)+','+CONVERT(varchar,YEAR(@DATE)-1)
--Special case: Dec 29-31 may belong to the next year
IF ((DATEPART(mm,@DATE)=12) AND
((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
SET @ISOweek='1'
PRINT (@ISOweek)


Regards /Snedker
 
1 January 2009 is a Thursday and there are 4 days of January 2009 before the
first Monday in January 2009, therefore the week number is 1 which is
correct.

Maybe you need to tell us what the 'Denmark specific' requirement is as it
appears that the 'Denmark specific' requirement does not comply with ISO
8601.
 
1 January 2009 is a Thursday and there are 4 days of January 2009 before the
first Monday in January 2009, therefore the week number is 1 which is
correct.

Maybe you need to tell us what the 'Denmark specific' requirement is as it
appears that the 'Denmark specific' requirement does not comply with ISO
8601.

You're right and I'm wrong. The code works fine, I just gave a
bad/wrong example.

I just have to put up a set of rules so I can figute out which year
the week belongs to.

With my CultureInfo 1/1/2010 is week 53, but week 53 belongs to 2008.

Regards /Snedker
 
With my CultureInfo 1/1/2010 is week 53, but week 53 belongs to 2008.

Jeez... 1/1/2010 is week 53, but belongs to 2009, of course, not 2008.

I meant what i didn't say. ;-)

Regards /Snedker
 
Wrong again!

1 January 2010 is in 53 of 2009 (not 2008).

The rule is simple and hold true for any year.

Public Structure ISO8601WeekYear

Public Week As Integer
Public Year As Integer

End Structure

Public Function GetISO8601WeekYear(ByVal date As DateTime) As
ISO8601WeekYear

Dim _iso8601weekyear As ISO8601WeekYear

Dim gc As New GregorianCalendar(GregorianCalendarTypes.Localized)

_iso8601weekyear.Week = gc.GetWeekOfYear(date,
CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

_iso8601weekyear.Year = date.Year

If _iso8601weekyear.Week > 1 AndAlso date.Month = 1 AndAlso date.Day < 5
Then _iso8601weekyear.Year -=1

Return iso8601weekyear

End Function
 
Back
Top