Number of weeks in year?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tommy Jakobsen

    Number of weeks in year?

    Hi.

    Is there a method in .NET that takes "year" as an argument and returns the total
    number of weeks in that year? For culture da-DK (Danish).

    Thanks in advance.

    Tommy.
  • Anthony Jones

    #2
    Re: Number of weeks in year?

    "Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
    news:1m8vc4pr3t hq7pl7n75u7p20f n7bd4ms6o@4ax.c om...
    Hi.
    >
    Is there a method in .NET that takes "year" as an argument and returns the
    total
    number of weeks in that year? For culture da-DK (Danish).
    >
    float w = (DateTime.IsLea pYear(year) ? 366 : 365) / 7

    Or are do you measure the count of weeks by the appearance of a specific day
    of the week considered to be the start of a week in that year?

    --
    Anthony Jones - MVP ASP/ASP.NET

    Comment

    • Clint

      #3
      Re: Number of weeks in year?

      Is there something special about the Danish calendar? i.e. is the answer not
      always 52? (plus a couple of days)

      "Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
      news:1m8vc4pr3t hq7pl7n75u7p20f n7bd4ms6o@4ax.c om...
      Hi.
      >
      Is there a method in .NET that takes "year" as an argument and returns the
      total
      number of weeks in that year? For culture da-DK (Danish).
      >
      Thanks in advance.
      >
      Tommy.

      Comment

      • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

        #4
        Re: Number of weeks in year?


        "Clint" wrote:
        Is there something special about the Danish calendar? i.e. is the answer not
        always 52? (plus a couple of days)
        >
        "Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
        news:1m8vc4pr3t hq7pl7n75u7p20f n7bd4ms6o@4ax.c om...
        Hi.

        Is there a method in .NET that takes "year" as an argument and returns the
        total
        number of weeks in that year? For culture da-DK (Danish).

        Thanks in advance.

        Tommy.
        >
        Sometimes there are 53 weeks in a year.

        int year = 2004;
        DateTime dt = new DateTime(year, 12, 31);
        int week = CultureInfo.Cur rentCulture.Cal endar.GetWeekOf Year(
        dt,
        CalendarWeekRul e.FirstFourDayW eek,
        DayOfWeek.Monda y);


        // week == 53

        --
        Happy Coding!
        Morten Wennevik [C# MVP]

        Comment

        • Tommy Jakobsen

          #5
          Re: Number of weeks in year?

          Hi Clint.

          That doesn't work.

          Theres an error in the framework. Using date DateTime(2008, 12, 31) returns 53,
          but theres only 52 weeks in year 2008.

          Any idea?

          On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
          <MortenWennevik @hotmail.comwro te:
          >
          >"Clint" wrote:
          >
          >Is there something special about the Danish calendar? i.e. is the answer not
          >always 52? (plus a couple of days)
          >>
          >"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
          >news:1m8vc4pr3 thq7pl7n75u7p20 fn7bd4ms6o@4ax. com...
          Hi.
          >
          Is there a method in .NET that takes "year" as an argument and returns the
          total
          number of weeks in that year? For culture da-DK (Danish).
          >
          Thanks in advance.
          >
          Tommy.
          >>
          >
          >Sometimes there are 53 weeks in a year.
          >
          int year = 2004;
          DateTime dt = new DateTime(year, 12, 31);
          int week = CultureInfo.Cur rentCulture.Cal endar.GetWeekOf Year(
          dt,
          CalendarWeekRul e.FirstFourDayW eek,
          DayOfWeek.Monda y);
          >
          >
          // week == 53

          Comment

          • TAB

            #6
            Re: Number of weeks in year?

            Hi Tommy

            I had the same problem when I was working with a calendarprogram and found
            this somewhere on the Internet.
            Apperently there is an old bug .Net that hasn't be solved yet.
            This is working for me, I have checked several years back and forth.

            // get week number for current date
            public int WeekNumber(Date Time fromDate)
            {
            // Get jan 1st of the year
            DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
            1).AddMonths(-fromDate.Month + 1);
            // Get dec 31st of the year
            DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
            // ISO 8601 weeks start with Monday
            // The first week of a year includes the first Thursday, i.e. at
            least 4 days
            // DayOfWeek returns 0 for sunday up to 6 for Saturday
            int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
            int nds = fromDate.Subtra ct(startOfYear) .Days +
            iso8601Correcti on[(int)startOfYea r.DayOfWeek];
            int wk = nds / 7;
            switch (wk)
            {
            case 0:
            // Return weeknumber of dec 31st of the previous year
            return WeekNumber(star tOfYear.AddDays (-1));
            case 53:
            // If dec 31st falls before thursday it is week 01 of
            next year
            if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
            return 1;
            else
            return wk;
            default: return wk;
            }

            "Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
            news:n0fvc4t3t6 4tc5509d9v7ulvc da9uq8vqi@4ax.c om...
            Hi Clint.
            >
            That doesn't work.
            >
            Theres an error in the framework. Using date DateTime(2008, 12, 31)
            returns 53,
            but theres only 52 weeks in year 2008.
            >
            Any idea?
            >
            On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
            <MortenWennevik @hotmail.comwro te:
            >
            >>
            >>"Clint" wrote:
            >>
            >>Is there something special about the Danish calendar? i.e. is the answer
            >>not
            >>always 52? (plus a couple of days)
            >>>
            >>"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
            >>news:1m8vc4pr 3thq7pl7n75u7p2 0fn7bd4ms6o@4ax .com...
            >Hi.
            >>
            >Is there a method in .NET that takes "year" as an argument and returns
            >the
            >total
            >number of weeks in that year? For culture da-DK (Danish).
            >>
            >Thanks in advance.
            >>
            >Tommy.
            >>>
            >>
            >>Sometimes there are 53 weeks in a year.
            >>
            > int year = 2004;
            > DateTime dt = new DateTime(year, 12, 31);
            > int week = CultureInfo.Cur rentCulture.Cal endar.GetWeekOf Year(
            > dt,
            > CalendarWeekRul e.FirstFourDayW eek,
            > DayOfWeek.Monda y);
            >>
            >>
            > // week == 53

            Comment

            • Tommy Jakobsen

              #7
              Re: Number of weeks in year?

              Thank you. Thats working yes, but it doesn't return the number of weeks in the
              year. It returns the correct number of the week at day 2008-12-31 (week 1, not
              53).

              How can I modify this to return the number of weeks in the year (52 or 53)?

              On Tue, 16 Sep 2008 16:27:09 +0200, "TAB" <anders.berglun d@email.comwrot e:
              >Hi Tommy
              >
              >I had the same problem when I was working with a calendarprogram and found
              >this somewhere on the Internet.
              >Apperently there is an old bug .Net that hasn't be solved yet.
              >This is working for me, I have checked several years back and forth.
              >
              // get week number for current date
              public int WeekNumber(Date Time fromDate)
              {
              // Get jan 1st of the year
              DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
              >1).AddMonths (-fromDate.Month + 1);
              // Get dec 31st of the year
              DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
              // ISO 8601 weeks start with Monday
              // The first week of a year includes the first Thursday, i.e. at
              >least 4 days
              // DayOfWeek returns 0 for sunday up to 6 for Saturday
              int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
              int nds = fromDate.Subtra ct(startOfYear) .Days +
              >iso8601Correct ion[(int)startOfYea r.DayOfWeek];
              int wk = nds / 7;
              switch (wk)
              {
              case 0:
              // Return weeknumber of dec 31st of the previous year
              return WeekNumber(star tOfYear.AddDays (-1));
              case 53:
              // If dec 31st falls before thursday it is week 01 of
              >next year
              if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
              return 1;
              else
              return wk;
              default: return wk;
              }
              >
              >"Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
              >news:n0fvc4t3t 64tc5509d9v7ulv cda9uq8vqi@4ax. com...
              >Hi Clint.
              >>
              >That doesn't work.
              >>
              >Theres an error in the framework. Using date DateTime(2008, 12, 31)
              >returns 53,
              >but theres only 52 weeks in year 2008.
              >>
              >Any idea?
              >>
              >On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
              ><MortenWennevi k@hotmail.comwr ote:
              >>
              >>>
              >>>"Clint" wrote:
              >>>
              >>>Is there something special about the Danish calendar? i.e. is the answer
              >>>not
              >>>always 52? (plus a couple of days)
              >>>>
              >>>"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
              >>>news:1m8vc4p r3thq7pl7n75u7p 20fn7bd4ms6o@4a x.com...
              >>Hi.
              >>>
              >>Is there a method in .NET that takes "year" as an argument and returns
              >>the
              >>total
              >>number of weeks in that year? For culture da-DK (Danish).
              >>>
              >>Thanks in advance.
              >>>
              >>Tommy.
              >>>>
              >>>
              >>>Sometimes there are 53 weeks in a year.
              >>>
              >> int year = 2004;
              >> DateTime dt = new DateTime(year, 12, 31);
              >> int week = CultureInfo.Cur rentCulture.Cal endar.GetWeekOf Year(
              >> dt,
              >> CalendarWeekRul e.FirstFourDayW eek,
              >> DayOfWeek.Monda y);
              >>>
              >>>
              >> // week == 53

              Comment

              • TAB

                #8
                Re: Number of weeks in year?

                I think this is the source of what I wrote and the same method somewhat
                modified.

                "Simen Sandelien says that will
                produce results incompatible with ISO 8601"

                "He has this to say about that :"

                "My conclusion is that the builtin .NET FourDayWeekRule
                and the GetWeekOfYear() method do NOT produce
                week numbers according to ISO 8601."

                private int WeekNumber_Enti re4DayWeekRule( DateTime date)
                {

                const int JAN = 1;
                const int DEC = 12;
                const int LASTDAYOFDEC = 31;
                const int FIRSTDAYOFJAN = 1;
                const int THURSDAY = 4;
                bool ThursdayFlag = false;

                int DayOfYear = date.DayOfYear;

                int StartWeekDayOfY ear =
                (int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
                int EndWeekDayOfYea r =
                (int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;

                StartWeekDayOfY ear = StartWeekDayOfY ear;
                EndWeekDayOfYea r = EndWeekDayOfYea r;
                if( StartWeekDayOfY ear == 0)
                StartWeekDayOfY ear = 7;
                if( EndWeekDayOfYea r == 0)
                EndWeekDayOfYea r = 7;

                int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
                int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );

                if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
                ThursdayFlag = true;

                int FullWeeks = (int) Math.Ceiling((D ayOfYear -
                (DaysInFirstWee k))/7.0);

                int WeekNumber = FullWeeks;

                if (DaysInFirstWee k >= THURSDAY)
                WeekNumber = WeekNumber +1;

                if (WeekNumber 52 && !ThursdayFlag)
                WeekNumber = 1;

                if (WeekNumber == 0)
                WeekNumber = WeekNumber_Enti re4DayWeekRule(
                new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
                return WeekNumber;
                }

                "TAB" <anders.berglun d@email.comskre v i meddelandet
                news:e9bxOhAGJH A.2112@TK2MSFTN GP02.phx.gbl...
                Hi Tommy
                >
                I had the same problem when I was working with a calendarprogram and found
                this somewhere on the Internet.
                Apperently there is an old bug .Net that hasn't be solved yet.
                This is working for me, I have checked several years back and forth.
                >
                // get week number for current date
                public int WeekNumber(Date Time fromDate)
                {
                // Get jan 1st of the year
                DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
                1).AddMonths(-fromDate.Month + 1);
                // Get dec 31st of the year
                DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
                // ISO 8601 weeks start with Monday
                // The first week of a year includes the first Thursday, i.e.
                at least 4 days
                // DayOfWeek returns 0 for sunday up to 6 for Saturday
                int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
                int nds = fromDate.Subtra ct(startOfYear) .Days +
                iso8601Correcti on[(int)startOfYea r.DayOfWeek];
                int wk = nds / 7;
                switch (wk)
                {
                case 0:
                // Return weeknumber of dec 31st of the previous year
                return WeekNumber(star tOfYear.AddDays (-1));
                case 53:
                // If dec 31st falls before thursday it is week 01 of
                next year
                if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
                return 1;
                else
                return wk;
                default: return wk;
                }
                >
                "Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                news:n0fvc4t3t6 4tc5509d9v7ulvc da9uq8vqi@4ax.c om...
                >Hi Clint.
                >>
                >That doesn't work.
                >>
                >Theres an error in the framework. Using date DateTime(2008, 12, 31)
                >returns 53,
                >but theres only 52 weeks in year 2008.
                >>
                >Any idea?
                >>
                >On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
                ><MortenWennevi k@hotmail.comwr ote:
                >>
                >>>
                >>>"Clint" wrote:
                >>>
                >>>Is there something special about the Danish calendar? i.e. is the
                >>>answer not
                >>>always 52? (plus a couple of days)
                >>>>
                >>>"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
                >>>news:1m8vc4p r3thq7pl7n75u7p 20fn7bd4ms6o@4a x.com...
                >>Hi.
                >>>
                >>Is there a method in .NET that takes "year" as an argument and
                >>returns the
                >>total
                >>number of weeks in that year? For culture da-DK (Danish).
                >>>
                >>Thanks in advance.
                >>>
                >>Tommy.
                >>>>
                >>>
                >>>Sometimes there are 53 weeks in a year.
                >>>
                >> int year = 2004;
                >> DateTime dt = new DateTime(year, 12, 31);
                >> int week = CultureInfo.Cur rentCulture.Cal endar.GetWeekOf Year(
                >> dt,
                >> CalendarWeekRul e.FirstFourDayW eek,
                >> DayOfWeek.Monda y);
                >>>
                >>>
                >> // week == 53
                >

                Comment

                • TAB

                  #9
                  Re: Number of weeks in year?



                  "Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                  news:t2hvc4dlrt mv6f3nmc1fv998k ovoomo33q@4ax.c om...
                  Thank you. Thats working yes, but it doesn't return the number of weeks in
                  the
                  year. It returns the correct number of the week at day 2008-12-31 (week 1,
                  not
                  53).
                  >
                  How can I modify this to return the number of weeks in the year (52 or
                  53)?
                  >
                  On Tue, 16 Sep 2008 16:27:09 +0200, "TAB" <anders.berglun d@email.com>
                  wrote:
                  >
                  >>Hi Tommy
                  >>
                  >>I had the same problem when I was working with a calendarprogram and found
                  >>this somewhere on the Internet.
                  >>Apperently there is an old bug .Net that hasn't be solved yet.
                  >>This is working for me, I have checked several years back and forth.
                  >>
                  > // get week number for current date
                  > public int WeekNumber(Date Time fromDate)
                  > {
                  > // Get jan 1st of the year
                  > DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
                  >>1).AddMonth s(-fromDate.Month + 1);
                  > // Get dec 31st of the year
                  > DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
                  > // ISO 8601 weeks start with Monday
                  > // The first week of a year includes the first Thursday, i.e.
                  >at
                  >>least 4 days
                  > // DayOfWeek returns 0 for sunday up to 6 for Saturday
                  > int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
                  > int nds = fromDate.Subtra ct(startOfYear) .Days +
                  >>iso8601Correc tion[(int)startOfYea r.DayOfWeek];
                  > int wk = nds / 7;
                  > switch (wk)
                  > {
                  > case 0:
                  > // Return weeknumber of dec 31st of the previous year
                  > return WeekNumber(star tOfYear.AddDays (-1));
                  > case 53:
                  > // If dec 31st falls before thursday it is week 01 of
                  >>next year
                  > if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
                  > return 1;
                  > else
                  > return wk;
                  > default: return wk;
                  > }
                  >>
                  >>"Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                  >>news:n0fvc4t3 t64tc5509d9v7ul vcda9uq8vqi@4ax .com...
                  >>Hi Clint.
                  >>>
                  >>That doesn't work.
                  >>>
                  >>Theres an error in the framework. Using date DateTime(2008, 12, 31)
                  >>returns 53,
                  >>but theres only 52 weeks in year 2008.
                  >>>
                  >>Any idea?
                  >>>
                  >>On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
                  >><MortenWennev ik@hotmail.comw rote:
                  >>>
                  >>>>
                  >>>>"Clint" wrote:
                  >>>>
                  >>>>Is there something special about the Danish calendar? i.e. is the
                  >>>>answer
                  >>>>not
                  >>>>always 52? (plus a couple of days)
                  >>>>>
                  >>>>"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
                  >>>>news:1m8vc4 pr3thq7pl7n75u7 p20fn7bd4ms6o@4 ax.com...
                  >>>Hi.
                  >>>>
                  >>>Is there a method in .NET that takes "year" as an argument and
                  >>>returns
                  >>>the
                  >>>total
                  >>>number of weeks in that year? For culture da-DK (Danish).
                  >>>>
                  >>>Thanks in advance.
                  >>>>
                  >>>Tommy.
                  >>>>>
                  >>>>
                  >>>>Sometimes there are 53 weeks in a year.
                  >>>>
                  >>> int year = 2004;
                  >>> DateTime dt = new DateTime(year, 12, 31);
                  >>> int week =
                  >>>CultureInfo. CurrentCulture. Calendar.GetWee kOfYear(
                  >>> dt,
                  >>> CalendarWeekRul e.FirstFourDayW eek,
                  >>> DayOfWeek.Monda y);
                  >>>>
                  >>>>
                  >>> // week == 53

                  Comment

                  • TAB

                    #10
                    Re: Number of weeks in year?

                    One way would be to do a loop and subtract 1 day until you find a weeknumber
                    bigger than 1, that would be 53 or 52.

                    "Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                    news:t2hvc4dlrt mv6f3nmc1fv998k ovoomo33q@4ax.c om...
                    Thank you. Thats working yes, but it doesn't return the number of weeks in
                    the
                    year. It returns the correct number of the week at day 2008-12-31 (week 1,
                    not
                    53).
                    >
                    How can I modify this to return the number of weeks in the year (52 or
                    53)?
                    >
                    On Tue, 16 Sep 2008 16:27:09 +0200, "TAB" <anders.berglun d@email.com>
                    wrote:
                    >
                    >>Hi Tommy
                    >>
                    >>I had the same problem when I was working with a calendarprogram and found
                    >>this somewhere on the Internet.
                    >>Apperently there is an old bug .Net that hasn't be solved yet.
                    >>This is working for me, I have checked several years back and forth.
                    >>
                    > // get week number for current date
                    > public int WeekNumber(Date Time fromDate)
                    > {
                    > // Get jan 1st of the year
                    > DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
                    >>1).AddMonth s(-fromDate.Month + 1);
                    > // Get dec 31st of the year
                    > DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
                    > // ISO 8601 weeks start with Monday
                    > // The first week of a year includes the first Thursday, i.e.
                    >at
                    >>least 4 days
                    > // DayOfWeek returns 0 for sunday up to 6 for Saturday
                    > int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
                    > int nds = fromDate.Subtra ct(startOfYear) .Days +
                    >>iso8601Correc tion[(int)startOfYea r.DayOfWeek];
                    > int wk = nds / 7;
                    > switch (wk)
                    > {
                    > case 0:
                    > // Return weeknumber of dec 31st of the previous year
                    > return WeekNumber(star tOfYear.AddDays (-1));
                    > case 53:
                    > // If dec 31st falls before thursday it is week 01 of
                    >>next year
                    > if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
                    > return 1;
                    > else
                    > return wk;
                    > default: return wk;
                    > }
                    >>
                    >>"Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                    >>news:n0fvc4t3 t64tc5509d9v7ul vcda9uq8vqi@4ax .com...
                    >>Hi Clint.
                    >>>
                    >>That doesn't work.
                    >>>
                    >>Theres an error in the framework. Using date DateTime(2008, 12, 31)
                    >>returns 53,
                    >>but theres only 52 weeks in year 2008.
                    >>>
                    >>Any idea?
                    >>>
                    >>On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
                    >><MortenWennev ik@hotmail.comw rote:
                    >>>
                    >>>>
                    >>>>"Clint" wrote:
                    >>>>
                    >>>>Is there something special about the Danish calendar? i.e. is the
                    >>>>answer
                    >>>>not
                    >>>>always 52? (plus a couple of days)
                    >>>>>
                    >>>>"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
                    >>>>news:1m8vc4 pr3thq7pl7n75u7 p20fn7bd4ms6o@4 ax.com...
                    >>>Hi.
                    >>>>
                    >>>Is there a method in .NET that takes "year" as an argument and
                    >>>returns
                    >>>the
                    >>>total
                    >>>number of weeks in that year? For culture da-DK (Danish).
                    >>>>
                    >>>Thanks in advance.
                    >>>>
                    >>>Tommy.
                    >>>>>
                    >>>>
                    >>>>Sometimes there are 53 weeks in a year.
                    >>>>
                    >>> int year = 2004;
                    >>> DateTime dt = new DateTime(year, 12, 31);
                    >>> int week =
                    >>>CultureInfo. CurrentCulture. Calendar.GetWee kOfYear(
                    >>> dt,
                    >>> CalendarWeekRul e.FirstFourDayW eek,
                    >>> DayOfWeek.Monda y);
                    >>>>
                    >>>>
                    >>> // week == 53

                    Comment

                    • Tommy Jakobsen

                      #11
                      Re: Number of weeks in year?

                      That method returns the same as the previous method, doesn't it?

                      It's still not what I'm looking for.



                      On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <anders.berglun d@email.comwrot e:
                      >I think this is the source of what I wrote and the same method somewhat
                      >modified.
                      >
                      >"Simen Sandelien says that will
                      >produce results incompatible with ISO 8601"
                      >
                      >"He has this to say about that :"
                      >
                      >"My conclusion is that the builtin .NET FourDayWeekRule
                      >and the GetWeekOfYear() method do NOT produce
                      >week numbers according to ISO 8601."
                      >
                      >private int WeekNumber_Enti re4DayWeekRule( DateTime date)
                      >{
                      >
                      const int JAN = 1;
                      const int DEC = 12;
                      const int LASTDAYOFDEC = 31;
                      const int FIRSTDAYOFJAN = 1;
                      const int THURSDAY = 4;
                      bool ThursdayFlag = false;
                      >
                      int DayOfYear = date.DayOfYear;
                      >
                      int StartWeekDayOfY ear =
                      (int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
                      int EndWeekDayOfYea r =
                      (int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;
                      >
                      StartWeekDayOfY ear = StartWeekDayOfY ear;
                      EndWeekDayOfYea r = EndWeekDayOfYea r;
                      if( StartWeekDayOfY ear == 0)
                      StartWeekDayOfY ear = 7;
                      if( EndWeekDayOfYea r == 0)
                      EndWeekDayOfYea r = 7;
                      >
                      int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
                      int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );
                      >
                      if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
                      ThursdayFlag = true;
                      >
                      int FullWeeks = (int) Math.Ceiling((D ayOfYear -
                      >(DaysInFirstWe ek))/7.0);
                      >
                      int WeekNumber = FullWeeks;
                      >
                      if (DaysInFirstWee k >= THURSDAY)
                      WeekNumber = WeekNumber +1;
                      >
                      if (WeekNumber 52 && !ThursdayFlag)
                      WeekNumber = 1;
                      >
                      if (WeekNumber == 0)
                      WeekNumber = WeekNumber_Enti re4DayWeekRule(
                      new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
                      return WeekNumber;
                      >}
                      >
                      >"TAB" <anders.berglun d@email.comskre v i meddelandet
                      >news:e9bxOhAGJ HA.2112@TK2MSFT NGP02.phx.gbl.. .
                      >Hi Tommy
                      >>
                      >I had the same problem when I was working with a calendarprogram and found
                      >this somewhere on the Internet.
                      >Apperently there is an old bug .Net that hasn't be solved yet.
                      >This is working for me, I have checked several years back and forth.
                      >>
                      > // get week number for current date
                      > public int WeekNumber(Date Time fromDate)
                      > {
                      > // Get jan 1st of the year
                      > DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
                      >1).AddMonths (-fromDate.Month + 1);
                      > // Get dec 31st of the year
                      > DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
                      > // ISO 8601 weeks start with Monday
                      > // The first week of a year includes the first Thursday, i.e.
                      >at least 4 days
                      > // DayOfWeek returns 0 for sunday up to 6 for Saturday
                      > int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
                      > int nds = fromDate.Subtra ct(startOfYear) .Days +
                      >iso8601Correct ion[(int)startOfYea r.DayOfWeek];
                      > int wk = nds / 7;
                      > switch (wk)
                      > {
                      > case 0:
                      > // Return weeknumber of dec 31st of the previous year
                      > return WeekNumber(star tOfYear.AddDays (-1));
                      > case 53:
                      > // If dec 31st falls before thursday it is week 01 of
                      >next year
                      > if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
                      > return 1;
                      > else
                      > return wk;
                      > default: return wk;
                      > }
                      >>
                      >"Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                      >news:n0fvc4t3t 64tc5509d9v7ulv cda9uq8vqi@4ax. com...
                      >>Hi Clint.
                      >>>
                      >>That doesn't work.
                      >>>
                      >>Theres an error in the framework. Using date DateTime(2008, 12, 31)
                      >>returns 53,
                      >>but theres only 52 weeks in year 2008.
                      >>>
                      >>Any idea?
                      >>>
                      >>On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
                      >><MortenWennev ik@hotmail.comw rote:
                      >>>
                      >>>>
                      >>>>"Clint" wrote:
                      >>>>
                      >>>>Is there something special about the Danish calendar? i.e. is the
                      >>>>answer not
                      >>>>always 52? (plus a couple of days)
                      >>>>>
                      >>>>"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
                      >>>>news:1m8vc4 pr3thq7pl7n75u7 p20fn7bd4ms6o@4 ax.com...
                      >>>Hi.
                      >>>>
                      >>>Is there a method in .NET that takes "year" as an argument and
                      >>>returns the
                      >>>total
                      >>>number of weeks in that year? For culture da-DK (Danish).
                      >>>>
                      >>>Thanks in advance.
                      >>>>
                      >>>Tommy.
                      >>>>>
                      >>>>
                      >>>>Sometimes there are 53 weeks in a year.
                      >>>>
                      >>> int year = 2004;
                      >>> DateTime dt = new DateTime(year, 12, 31);
                      >>> int week = CultureInfo.Cur rentCulture.Cal endar.GetWeekOf Year(
                      >>> dt,
                      >>> CalendarWeekRul e.FirstFourDayW eek,
                      >>> DayOfWeek.Monda y);
                      >>>>
                      >>>>
                      >>> // week == 53
                      >>

                      Comment

                      • TAB

                        #12
                        Re: Number of weeks in year?

                        Start with the last day of the year and subtract on day at a time until you
                        find the previous week, whatever it may be.


                        "Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                        news:44ivc4p9kl 7ucicnojpaqogj1 3sqnt6nr7@4ax.c om...
                        That method returns the same as the previous method, doesn't it?
                        >
                        It's still not what I'm looking for.
                        >
                        >
                        >
                        On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <anders.berglun d@email.com>
                        wrote:
                        >
                        >>I think this is the source of what I wrote and the same method somewhat
                        >>modified.
                        >>
                        >>"Simen Sandelien says that will
                        >>produce results incompatible with ISO 8601"
                        >>
                        >>"He has this to say about that :"
                        >>
                        >>"My conclusion is that the builtin .NET FourDayWeekRule
                        >>and the GetWeekOfYear() method do NOT produce
                        >>week numbers according to ISO 8601."
                        >>
                        >>private int WeekNumber_Enti re4DayWeekRule( DateTime date)
                        >>{
                        >>
                        > const int JAN = 1;
                        > const int DEC = 12;
                        > const int LASTDAYOFDEC = 31;
                        > const int FIRSTDAYOFJAN = 1;
                        > const int THURSDAY = 4;
                        > bool ThursdayFlag = false;
                        >>
                        > int DayOfYear = date.DayOfYear;
                        >>
                        > int StartWeekDayOfY ear =
                        > (int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
                        > int EndWeekDayOfYea r =
                        > (int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;
                        >>
                        > StartWeekDayOfY ear = StartWeekDayOfY ear;
                        > EndWeekDayOfYea r = EndWeekDayOfYea r;
                        > if( StartWeekDayOfY ear == 0)
                        > StartWeekDayOfY ear = 7;
                        > if( EndWeekDayOfYea r == 0)
                        > EndWeekDayOfYea r = 7;
                        >>
                        > int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
                        > int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );
                        >>
                        > if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
                        > ThursdayFlag = true;
                        >>
                        > int FullWeeks = (int) Math.Ceiling((D ayOfYear -
                        >>(DaysInFirstW eek))/7.0);
                        >>
                        > int WeekNumber = FullWeeks;
                        >>
                        > if (DaysInFirstWee k >= THURSDAY)
                        > WeekNumber = WeekNumber +1;
                        >>
                        > if (WeekNumber 52 && !ThursdayFlag)
                        > WeekNumber = 1;
                        >>
                        > if (WeekNumber == 0)
                        > WeekNumber = WeekNumber_Enti re4DayWeekRule(
                        > new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
                        > return WeekNumber;
                        >>}
                        >>
                        >>"TAB" <anders.berglun d@email.comskre v i meddelandet
                        >>news:e9bxOhAG JHA.2112@TK2MSF TNGP02.phx.gbl. ..
                        >>Hi Tommy
                        >>>
                        >>I had the same problem when I was working with a calendarprogram and
                        >>found
                        >>this somewhere on the Internet.
                        >>Apperently there is an old bug .Net that hasn't be solved yet.
                        >>This is working for me, I have checked several years back and forth.
                        >>>
                        >> // get week number for current date
                        >> public int WeekNumber(Date Time fromDate)
                        >> {
                        >> // Get jan 1st of the year
                        >> DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
                        >>1).AddMonth s(-fromDate.Month + 1);
                        >> // Get dec 31st of the year
                        >> DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
                        >> // ISO 8601 weeks start with Monday
                        >> // The first week of a year includes the first Thursday, i.e.
                        >>at least 4 days
                        >> // DayOfWeek returns 0 for sunday up to 6 for Saturday
                        >> int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
                        >> int nds = fromDate.Subtra ct(startOfYear) .Days +
                        >>iso8601Correc tion[(int)startOfYea r.DayOfWeek];
                        >> int wk = nds / 7;
                        >> switch (wk)
                        >> {
                        >> case 0:
                        >> // Return weeknumber of dec 31st of the previous year
                        >> return WeekNumber(star tOfYear.AddDays (-1));
                        >> case 53:
                        >> // If dec 31st falls before thursday it is week 01 of
                        >>next year
                        >> if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
                        >> return 1;
                        >> else
                        >> return wk;
                        >> default: return wk;
                        >> }
                        >>>
                        >>"Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                        >>news:n0fvc4t3 t64tc5509d9v7ul vcda9uq8vqi@4ax .com...
                        >>>Hi Clint.
                        >>>>
                        >>>That doesn't work.
                        >>>>
                        >>>Theres an error in the framework. Using date DateTime(2008, 12, 31)
                        >>>returns 53,
                        >>>but theres only 52 weeks in year 2008.
                        >>>>
                        >>>Any idea?
                        >>>>
                        >>>On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
                        >>><MortenWenne vik@hotmail.com wrote:
                        >>>>
                        >>>>>
                        >>>>>"Clint" wrote:
                        >>>>>
                        >>>>>Is there something special about the Danish calendar? i.e. is the
                        >>>>>answer not
                        >>>>>always 52? (plus a couple of days)
                        >>>>>>
                        >>>>>"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
                        >>>>>news:1m8vc 4pr3thq7pl7n75u 7p20fn7bd4ms6o@ 4ax.com...
                        >>>>Hi.
                        >>>>>
                        >>>>Is there a method in .NET that takes "year" as an argument and
                        >>>>returns the
                        >>>>total
                        >>>>number of weeks in that year? For culture da-DK (Danish).
                        >>>>>
                        >>>>Thanks in advance.
                        >>>>>
                        >>>>Tommy.
                        >>>>>>
                        >>>>>
                        >>>>>Sometime s there are 53 weeks in a year.
                        >>>>>
                        >>>> int year = 2004;
                        >>>> DateTime dt = new DateTime(year, 12, 31);
                        >>>> int week =
                        >>>>CultureInfo .CurrentCulture .Calendar.GetWe ekOfYear(
                        >>>> dt,
                        >>>> CalendarWeekRul e.FirstFourDayW eek,
                        >>>> DayOfWeek.Monda y);
                        >>>>>
                        >>>>>
                        >>>> // week == 53
                        >>>

                        Comment

                        • G.S.

                          #13
                          Re: Number of weeks in year?

                          On Sep 16, 10:54 am, Tommy Jakobsen <to...@holmjako bsen.dkwrote:
                          That method returns the same as the previous method, doesn't it?
                          >
                          It's still not what I'm looking for.
                          >
                          >
                          >
                          On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <anders.bergl.. .@email.comwrot e:
                          I think this is the source of what I wrote and the same method somewhat
                          modified.
                          >
                          "Simen Sandelien says that will
                          produce results incompatible with ISO 8601"
                          >
                          "He has this to say about that :"
                          >
                          "My conclusion is that the builtin .NET FourDayWeekRule
                          and the GetWeekOfYear() method do NOT produce
                          week numbers according to ISO 8601."
                          >
                          private int WeekNumber_Enti re4DayWeekRule( DateTime date)
                          {
                          >
                              const int JAN = 1;
                              const int DEC = 12;
                              const int LASTDAYOFDEC = 31;
                              const int FIRSTDAYOFJAN = 1;
                              const int THURSDAY = 4;
                              bool ThursdayFlag = false;
                          >
                              int DayOfYear = date.DayOfYear;
                          >
                              int StartWeekDayOfY ear =
                                   (int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
                              int EndWeekDayOfYea r =
                                   (int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;
                          >
                              StartWeekDayOfY ear = StartWeekDayOfY ear;
                              EndWeekDayOfYea r = EndWeekDayOfYea r;
                              if( StartWeekDayOfY ear == 0)
                                   StartWeekDayOfY ear = 7;
                              if( EndWeekDayOfYea r == 0)
                                   EndWeekDayOfYea r = 7;
                          >
                              int DaysInFirstWeek = 8 - (StartWeekDayOf Year  );
                              int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );
                          >
                              if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
                                   ThursdayFlag = true;
                          >
                              int FullWeeks = (int) Math.Ceiling((D ayOfYear -
                          (DaysInFirstWee k))/7.0);
                          >
                              int WeekNumber = FullWeeks;
                          >
                              if (DaysInFirstWee k >= THURSDAY)
                                   WeekNumber = WeekNumber +1;
                          >
                              if (WeekNumber 52 && !ThursdayFlag)
                                   WeekNumber = 1;
                          >
                              if (WeekNumber == 0)
                                   WeekNumber = WeekNumber_Enti re4DayWeekRule(
                                        new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
                              return WeekNumber;
                          }
                          >
                          "TAB" <anders.bergl.. .@email.comskre v i meddelandet
                          news:e9bxOhAGJH A.2112@TK2MSFTN GP02.phx.gbl...
                          Hi Tommy
                          >
                          I had the same problem when I was working with a calendarprogram and found
                          this somewhere on the Internet.
                          Apperently there is an old bug .Net that hasn't be solved yet.
                          This is working for me, I have checked several years back and forth.
                          >
                                 // get week number for current date
                                 public int WeekNumber(Date Time fromDate)
                                 {
                                     // Get jan 1st of the year
                                     DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
                          1).AddMonths(-fromDate.Month + 1);
                                     // Get dec 31st of the year
                                     DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
                                     // ISO 8601 weeks start with Monday
                                     // The first week of a year includes the first Thursday, i.e.
                          at least 4 days
                                     // DayOfWeek returns 0 for sunday up to 6 for Saturday
                                     int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
                                     int nds = fromDate.Subtra ct(startOfYear) .Days+
                          iso8601Correcti on[(int)startOfYea r.DayOfWeek];
                                     int wk = nds / 7;
                                     switch (wk)
                                     {
                                         case 0:
                                             // Return weeknumber of dec 31st of the previous year
                                             return WeekNumber(star tOfYear.AddDays (-1));
                                         case 53:
                                             // If dec 31st falls before thursday it is week 01 of
                          next year
                                             if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
                                                 return 1;
                                             else
                                                 return wk;
                                         default: return wk;
                                     }
                          >
                          "Tommy Jakobsen" <to...@holmjako bsen.dkskrev i meddelandet
                          >news:n0fvc4t3t 64tc5509d9v7ulv cda9uq8vqi@4ax. com...
                          >Hi Clint.
                          >
                          >That doesn't work.
                          >
                          >Theres an error in the framework. Using date DateTime(2008, 12, 31)
                          >returns 53,
                          >but theres only 52 weeks in year 2008.
                          >
                          >Any idea?
                          >
                          >On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
                          ><MortenWenne.. .@hotmail.comwr ote:
                          >
                          >>>"Clint" wrote:
                          >
                          >>>Is there something special about the Danish calendar? i.e. is the
                          >>>answer not
                          >>>always 52? (plus a couple of days)
                          >
                          >>>"Tommy Jakobsen" <to...@holmjako bsen.dkwrote in message
                          >>>>news:1m8vc4 pr3thq7pl7n75u7 p20fn7bd4ms6o@4 ax.com...
                          >>Hi.
                          >
                          >>Is there a method in .NET that takes "year" as an argument and
                          >>returns the
                          >>total
                          >>number of weeks in that year? For culture da-DK (Danish).
                          >
                          >>Thanks in advance.
                          >
                          >>Tommy.
                          >
                          >>>Sometimes there are 53 weeks in a year.
                          >
                          >>           int year = 2004;
                          >>           DateTime dt = new DateTime(year, 12, 31);
                          >>           int week = CultureInfo.Cur rentCulture.Cal endar.GetWeekOf Year(
                          >>               dt,
                          >>               CalendarWeekRul e.FirstFourDayW eek,
                          >>               DayOfWeek.Monda y);
                          >
                          >>           // week == 53- Hide quoted text -
                          >
                          - Show quoted text -
                          I'd echo firts responder's question - what do you consider the first
                          week?

                          The example in this article should clarify things a bit:

                          Comment

                          • Tommy Jakobsen

                            #14
                            Re: Number of weeks in year?

                            I got it modified to take year as parameter and return the total number of weeks
                            (52/53) in that year.

                            Thanks for your replies guys.

                            On Tue, 16 Sep 2008 17:18:20 +0200, "TAB" <anders.berglun d@email.comwrot e:
                            >Start with the last day of the year and subtract on day at a time until you
                            >find the previous week, whatever it may be.
                            >
                            >
                            >"Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                            >news:44ivc4p9k l7ucicnojpaqogj 13sqnt6nr7@4ax. com...
                            >That method returns the same as the previous method, doesn't it?
                            >>
                            >It's still not what I'm looking for.
                            >>
                            >>
                            >>
                            >On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <anders.berglun d@email.com>
                            >wrote:
                            >>
                            >>>I think this is the source of what I wrote and the same method somewhat
                            >>>modified.
                            >>>
                            >>>"Simen Sandelien says that will
                            >>>produce results incompatible with ISO 8601"
                            >>>
                            >>>"He has this to say about that :"
                            >>>
                            >>>"My conclusion is that the builtin .NET FourDayWeekRule
                            >>>and the GetWeekOfYear() method do NOT produce
                            >>>week numbers according to ISO 8601."
                            >>>
                            >>>private int WeekNumber_Enti re4DayWeekRule( DateTime date)
                            >>>{
                            >>>
                            >> const int JAN = 1;
                            >> const int DEC = 12;
                            >> const int LASTDAYOFDEC = 31;
                            >> const int FIRSTDAYOFJAN = 1;
                            >> const int THURSDAY = 4;
                            >> bool ThursdayFlag = false;
                            >>>
                            >> int DayOfYear = date.DayOfYear;
                            >>>
                            >> int StartWeekDayOfY ear =
                            >> (int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
                            >> int EndWeekDayOfYea r =
                            >> (int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;
                            >>>
                            >> StartWeekDayOfY ear = StartWeekDayOfY ear;
                            >> EndWeekDayOfYea r = EndWeekDayOfYea r;
                            >> if( StartWeekDayOfY ear == 0)
                            >> StartWeekDayOfY ear = 7;
                            >> if( EndWeekDayOfYea r == 0)
                            >> EndWeekDayOfYea r = 7;
                            >>>
                            >> int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
                            >> int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );
                            >>>
                            >> if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
                            >> ThursdayFlag = true;
                            >>>
                            >> int FullWeeks = (int) Math.Ceiling((D ayOfYear -
                            >>>(DaysInFirst Week))/7.0);
                            >>>
                            >> int WeekNumber = FullWeeks;
                            >>>
                            >> if (DaysInFirstWee k >= THURSDAY)
                            >> WeekNumber = WeekNumber +1;
                            >>>
                            >> if (WeekNumber 52 && !ThursdayFlag)
                            >> WeekNumber = 1;
                            >>>
                            >> if (WeekNumber == 0)
                            >> WeekNumber = WeekNumber_Enti re4DayWeekRule(
                            >> new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
                            >> return WeekNumber;
                            >>>}
                            >>>
                            >>>"TAB" <anders.berglun d@email.comskre v i meddelandet
                            >>>news:e9bxOhA GJHA.2112@TK2MS FTNGP02.phx.gbl ...
                            >>>Hi Tommy
                            >>>>
                            >>>I had the same problem when I was working with a calendarprogram and
                            >>>found
                            >>>this somewhere on the Internet.
                            >>>Apperently there is an old bug .Net that hasn't be solved yet.
                            >>>This is working for me, I have checked several years back and forth.
                            >>>>
                            >>> // get week number for current date
                            >>> public int WeekNumber(Date Time fromDate)
                            >>> {
                            >>> // Get jan 1st of the year
                            >>> DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
                            >>>1).AddMonths (-fromDate.Month + 1);
                            >>> // Get dec 31st of the year
                            >>> DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
                            >>> // ISO 8601 weeks start with Monday
                            >>> // The first week of a year includes the first Thursday, i.e.
                            >>>at least 4 days
                            >>> // DayOfWeek returns 0 for sunday up to 6 for Saturday
                            >>> int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
                            >>> int nds = fromDate.Subtra ct(startOfYear) .Days +
                            >>>iso8601Corre ction[(int)startOfYea r.DayOfWeek];
                            >>> int wk = nds / 7;
                            >>> switch (wk)
                            >>> {
                            >>> case 0:
                            >>> // Return weeknumber of dec 31st of the previous year
                            >>> return WeekNumber(star tOfYear.AddDays (-1));
                            >>> case 53:
                            >>> // If dec 31st falls before thursday it is week 01 of
                            >>>next year
                            >>> if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
                            >>> return 1;
                            >>> else
                            >>> return wk;
                            >>> default: return wk;
                            >>> }
                            >>>>
                            >>>"Tommy Jakobsen" <tommy@holmjako bsen.dkskrev i meddelandet
                            >>>news:n0fvc4t 3t64tc5509d9v7u lvcda9uq8vqi@4a x.com...
                            >>>>Hi Clint.
                            >>>>>
                            >>>>That doesn't work.
                            >>>>>
                            >>>>Theres an error in the framework. Using date DateTime(2008, 12, 31)
                            >>>>returns 53,
                            >>>>but theres only 52 weeks in year 2008.
                            >>>>>
                            >>>>Any idea?
                            >>>>>
                            >>>>On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
                            >>>><MortenWenn evik@hotmail.co mwrote:
                            >>>>>
                            >>>>>>
                            >>>>>>"Clint" wrote:
                            >>>>>>
                            >>>>>>Is there something special about the Danish calendar? i.e. is the
                            >>>>>>answer not
                            >>>>>>always 52? (plus a couple of days)
                            >>>>>>>
                            >>>>>>"Tommy Jakobsen" <tommy@holmjako bsen.dkwrote in message
                            >>>>>>news:1m8v c4pr3thq7pl7n75 u7p20fn7bd4ms6o @4ax.com...
                            >>>>>Hi.
                            >>>>>>
                            >>>>>Is there a method in .NET that takes "year" as an argument and
                            >>>>>returns the
                            >>>>>total
                            >>>>>number of weeks in that year? For culture da-DK (Danish).
                            >>>>>>
                            >>>>>Thanks in advance.
                            >>>>>>
                            >>>>>Tommy.
                            >>>>>>>
                            >>>>>>
                            >>>>>>Sometim es there are 53 weeks in a year.
                            >>>>>>
                            >>>>> int year = 2004;
                            >>>>> DateTime dt = new DateTime(year, 12, 31);
                            >>>>> int week =
                            >>>>>CultureInf o.CurrentCultur e.Calendar.GetW eekOfYear(
                            >>>>> dt,
                            >>>>> CalendarWeekRul e.FirstFourDayW eek,
                            >>>>> DayOfWeek.Monda y);
                            >>>>>>
                            >>>>>>
                            >>>>> // week == 53
                            >>>>

                            Comment

                            • TAB

                              #15
                              Re: Number of weeks in year?



                              "G.S." <gstoynev@gmail .comskrev i meddelandet
                              news:8169487c-e2cd-4a1e-b86c-8c5172aaba0d@e5 3g2000hsa.googl egroups.com...
                              On Sep 16, 10:54 am, Tommy Jakobsen <to...@holmjako bsen.dkwrote:
                              >That method returns the same as the previous method, doesn't it?
                              >>
                              >It's still not what I'm looking for.
                              >>
                              >>
                              >>
                              >On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <anders.bergl.. .@email.com>
                              >wrote:
                              >I think this is the source of what I wrote and the same method somewhat
                              >modified.
                              >>
                              >"Simen Sandelien says that will
                              >produce results incompatible with ISO 8601"
                              >>
                              >"He has this to say about that :"
                              >>
                              >"My conclusion is that the builtin .NET FourDayWeekRule
                              >and the GetWeekOfYear() method do NOT produce
                              >week numbers according to ISO 8601."
                              >>
                              >private int WeekNumber_Enti re4DayWeekRule( DateTime date)
                              >{
                              >>
                              const int JAN = 1;
                              const int DEC = 12;
                              const int LASTDAYOFDEC = 31;
                              const int FIRSTDAYOFJAN = 1;
                              const int THURSDAY = 4;
                              bool ThursdayFlag = false;
                              >>
                              int DayOfYear = date.DayOfYear;
                              >>
                              int StartWeekDayOfY ear =
                              (int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
                              int EndWeekDayOfYea r =
                              (int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;
                              >>
                              StartWeekDayOfY ear = StartWeekDayOfY ear;
                              EndWeekDayOfYea r = EndWeekDayOfYea r;
                              if( StartWeekDayOfY ear == 0)
                              StartWeekDayOfY ear = 7;
                              if( EndWeekDayOfYea r == 0)
                              EndWeekDayOfYea r = 7;
                              >>
                              int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
                              int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );
                              >>
                              if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
                              ThursdayFlag = true;
                              >>
                              int FullWeeks = (int) Math.Ceiling((D ayOfYear -
                              >(DaysInFirstWe ek))/7.0);
                              >>
                              int WeekNumber = FullWeeks;
                              >>
                              if (DaysInFirstWee k >= THURSDAY)
                              WeekNumber = WeekNumber +1;
                              >>
                              if (WeekNumber 52 && !ThursdayFlag)
                              WeekNumber = 1;
                              >>
                              if (WeekNumber == 0)
                              WeekNumber = WeekNumber_Enti re4DayWeekRule(
                              new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
                              return WeekNumber;
                              >}
                              >>
                              >"TAB" <anders.bergl.. .@email.comskre v i meddelandet
                              >news:e9bxOhAGJ HA.2112@TK2MSFT NGP02.phx.gbl.. .
                              >Hi Tommy
                              >>
                              >I had the same problem when I was working with a calendarprogram and
                              >found
                              >this somewhere on the Internet.
                              >Apperently there is an old bug .Net that hasn't be solved yet.
                              >This is working for me, I have checked several years back and forth.
                              >>
                              >// get week number for current date
                              >public int WeekNumber(Date Time fromDate)
                              >{
                              >// Get jan 1st of the year
                              >DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
                              >1).AddMonths (-fromDate.Month + 1);
                              >// Get dec 31st of the year
                              >DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-1);
                              >// ISO 8601 weeks start with Monday
                              >// The first week of a year includes the first Thursday, i.e.
                              >at least 4 days
                              >// DayOfWeek returns 0 for sunday up to 6 for Saturday
                              >int[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
                              >int nds = fromDate.Subtra ct(startOfYear) .Days +
                              >iso8601Correct ion[(int)startOfYea r.DayOfWeek];
                              >int wk = nds / 7;
                              >switch (wk)
                              >{
                              >case 0:
                              >// Return weeknumber of dec 31st of the previous year
                              >return WeekNumber(star tOfYear.AddDays (-1));
                              >case 53:
                              >// If dec 31st falls before thursday it is week 01 of
                              >next year
                              >if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
                              >return 1;
                              >else
                              >return wk;
                              >default: return wk;
                              >}
                              >>
                              >"Tommy Jakobsen" <to...@holmjako bsen.dkskrev i meddelandet
                              >>news:n0fvc4t3 t64tc5509d9v7ul vcda9uq8vqi@4ax .com...
                              >>Hi Clint.
                              >>
                              >>That doesn't work.
                              >>
                              >>Theres an error in the framework. Using date DateTime(2008, 12, 31)
                              >>returns 53,
                              >>but theres only 52 weeks in year 2008.
                              >>
                              >>Any idea?
                              >>
                              >>On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
                              >><MortenWenne. ..@hotmail.comw rote:
                              >>
                              >>>>"Clint" wrote:
                              >>
                              >>>>Is there something special about the Danish calendar? i.e. is the
                              >>>>answer not
                              >>>>always 52? (plus a couple of days)
                              >>
                              >>>>"Tommy Jakobsen" <to...@holmjako bsen.dkwrote in message
                              >>>>>news:1m8vc 4pr3thq7pl7n75u 7p20fn7bd4ms6o@ 4ax.com...
                              >>>Hi.
                              >>
                              >>>Is there a method in .NET that takes "year" as an argument and
                              >>>returns the
                              >>>total
                              >>>number of weeks in that year? For culture da-DK (Danish).
                              >>
                              >>>Thanks in advance.
                              >>
                              >>>Tommy.
                              >>
                              >>>>Sometimes there are 53 weeks in a year.
                              >>
                              >>>int year = 2004;
                              >>>DateTime dt = new DateTime(year, 12, 31);
                              >>>int week = CultureInfo.Cur rentCulture.Cal endar.GetWeekOf Year(
                              >>>dt,
                              >>>CalendarWeek Rule.FirstFourD ayWeek,
                              >>>DayOfWeek.Mo nday);
                              >>
                              >>>// week == 53- Hide quoted text -
                              >>
                              >- Show quoted text -
                              >
                              I'd echo firts responder's question - what do you consider the first
                              week?
                              >
                              The example in this article should clarify things a bit:
                              http://msdn.microsoft.com/en-us/libr...le(VS.95).aspx
                              It's been a long time since I worked with this so I have to quote
                              Wikipedia on ISO 8601 regarding week numbers.

                              The ISO year number deviates from the number of the Gregorian year on, if
                              applicable, a Friday, Saturday, and Sunday, or a Saturday and Sunday, or
                              just a Sunday, at the start of the Gregorian year (which are at the end of
                              the previous ISO year) and a Monday, Tuesday and Wednesday, or a Monday and
                              Tuesday, or just a Monday, at the end of the Gregorian year (which are in
                              week 01 of the next ISO year). In the period 4 January-28 December and on
                              all Thursdays the ISO year number is always equal to the Gregorian year
                              number.

                              Mutually equivalent definitions for week 01 are:
                              the week with the year's first Thursday in it
                              the week starting with the Monday which is nearest in time to 1 January
                              the week with the year's first working day in it (if Saturdays, Sundays, and
                              1 January are not working days)
                              the week with January 4 in it
                              the first week with the majority (four or more) of its days in the starting
                              year
                              the week starting with the Monday in the period 29 December - 4 January
                              the week with the Thursday in the period 1 - 7 January
                              If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week
                              01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53
                              of the previous year.
                              Note that while most definitions are symmetric with respect to time
                              reversal, one definition in terms of working days happens to be equivalent.

                              The last week of the ISO year is the week before week 01; in accordance with
                              the symmetry of the definition, equivalent definitions are:
                              the week with the year's last Thursday in it
                              the week ending with the Sunday which is nearest in time to 31 December
                              the week with December 28 in it
                              the last week with the majority (four or more) of its days in the ending
                              year
                              the week starting with the Monday in the period 22 - 28 December
                              the week with the Thursday in the period 25 - 31 December
                              the week ending with the Sunday in the period 28 December - 3 January
                              If 31 December is on a Monday, Tuesday, or Wednesday, it is in week 01 of
                              the next year, otherwise in week 52 or 53.
                              The following years have 53 weeks:
                              years starting with Thursday
                              leap years starting with Wednesday


                              Comment

                              Working...