Getting the weeknumber

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Morten Snedker

    #1

    Getting the weeknumber

    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.Globaliz ation.Gregorian Calendar

    gc.CalendarType = Globalization.G regorianCalenda rTypes.Localize d
    iWeek = gc.GetWeekOfYea r(#1/1/2009#,
    Globalization.C alendarWeekRule .FirstFourDayWe ek, DayOfWeek.Monda y)

    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,@D ATE)+1
    -DATEPART(wk,CAS T(DATEPART(yy,@ DATE) as CHAR(4))+'0104' ))
    --Special cases: Jan 1-3 may belong to the previous year
    IF (@ISOweek='0')
    SET
    @ISOweek=CONVER T(varchar,dbo.I SOweek(CAST(DAT EPART(yy,@DATE)-1
    AS CHAR(4))+'12'+ CAST(24+DATEPAR T(DAY,@DATE) AS
    CHAR(2)))+1)+', '+CONVERT(varch ar,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,@DA TE))>= 28))
    SET @ISOweek='1'
    PRINT (@ISOweek)


    Regards /Snedker
    --

    http://www.vinthervej2.dk [private]
  • Stephany Young

    #2
    Re: Getting the weeknumber

    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.


    "Morten Snedker" <morten_spammen ot_ATdbconsult. dkwrote in message
    news:dofl43l648 v22ltatoo8a3ark 4g5iaj90h@4ax.c om...
    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.Globaliz ation.Gregorian Calendar
    >
    gc.CalendarType = Globalization.G regorianCalenda rTypes.Localize d
    iWeek = gc.GetWeekOfYea r(#1/1/2009#,
    Globalization.C alendarWeekRule .FirstFourDayWe ek, DayOfWeek.Monda y)
    >
    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,@D ATE)+1
    -DATEPART(wk,CAS T(DATEPART(yy,@ DATE) as CHAR(4))+'0104' ))
    --Special cases: Jan 1-3 may belong to the previous year
    IF (@ISOweek='0')
    SET
    @ISOweek=CONVER T(varchar,dbo.I SOweek(CAST(DAT EPART(yy,@DATE)-1
    AS CHAR(4))+'12'+ CAST(24+DATEPAR T(DAY,@DATE) AS
    CHAR(2)))+1)+', '+CONVERT(varch ar,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,@DA TE))>= 28))
    SET @ISOweek='1'
    PRINT (@ISOweek)
    >
    >
    Regards /Snedker
    --

    http://www.vinthervej2.dk [private]

    Comment

    • Morten Snedker

      #3
      Re: Getting the weeknumber

      On Wed, 16 May 2007 20:52:38 +1200, "Stephany Young" <noone@localhos t>
      wrote:
      >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
      --

      http://www.vinthervej2.dk [private]

      Comment

      • Morten Snedker

        #4
        Re: Getting the weeknumber

        On Wed, 16 May 2007 11:06:00 +0200, Morten Snedker
        <morten_spammen ot_ATdbconsult. dkwrote:

        >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
        --

        http://www.vinthervej2.dk [private]

        Comment

        • Stephany Young

          #5
          Re: Getting the weeknumber

          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 GetISO8601WeekY ear(ByVal date As DateTime) As
          ISO8601WeekYear

          Dim _iso8601weekyea r As ISO8601WeekYear

          Dim gc As New GregorianCalend ar(GregorianCal endarTypes.Loca lized)

          _iso8601weekyea r.Week = gc.GetWeekOfYea r(date,
          CalendarWeekRul e.FirstFourDayW eek, DayOfWeek.Monda y)

          _iso8601weekyea r.Year = date.Year

          If _iso8601weekyea r.Week 1 AndAlso date.Month = 1 AndAlso date.Day < 5
          Then _iso8601weekyea r.Year -=1

          Return iso8601weekyear

          End Function


          "Morten Snedker" <morten_spammen ot_ATdbconsult. dkwrote in message
          news:j3il43tqv4 i29r83buo6ipimt 81h2iii25@4ax.c om...
          On Wed, 16 May 2007 20:52:38 +1200, "Stephany Young" <noone@localhos t>
          wrote:
          >
          >>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
          --

          http://www.vinthervej2.dk [private]

          Comment

          Working...