Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. find week num of first week in year [modified]

find week num of first week in year [modified]

Scheduled Pinned Locked Moved C#
question
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maddie from Dartford
    wrote on last edited by
    #1

    Hi, I want to know the first week num of Jan 2010 if first day of week is Monday. All day before monday should be considered under last week of previous year, i.e. As first monday comes on 4th Jan 2010, i.e. I am expecting Jan 1,2 & 3 under week 53 of last year. I am using this code

    System.Globalization.CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentCulture;
    System.Globalization.Calendar calendar = currentCulture.Calendar;
    DateTime firstOfYear = new DateTime(DateTime.Now.Year, 1, 1);

    int weekNo = calendar.GetWeekOfYear(firstOfYear, System.Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);

    The week num is coming 52 instead of 53. What is going wrong? Is there any other way to do it? Thanks.

    modified on Thursday, January 7, 2010 10:19 AM

    D L 2 Replies Last reply
    0
    • M Maddie from Dartford

      Hi, I want to know the first week num of Jan 2010 if first day of week is Monday. All day before monday should be considered under last week of previous year, i.e. As first monday comes on 4th Jan 2010, i.e. I am expecting Jan 1,2 & 3 under week 53 of last year. I am using this code

      System.Globalization.CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentCulture;
      System.Globalization.Calendar calendar = currentCulture.Calendar;
      DateTime firstOfYear = new DateTime(DateTime.Now.Year, 1, 1);

      int weekNo = calendar.GetWeekOfYear(firstOfYear, System.Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);

      The week num is coming 52 instead of 53. What is going wrong? Is there any other way to do it? Thanks.

      modified on Thursday, January 7, 2010 10:19 AM

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Change FirstFullWeek to FirstDay and you would get one. There is nothing like 53 week, AFAIK, so won't get that.

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      M OriginalGriffO 2 Replies Last reply
      0
      • D dan sh

        Change FirstFullWeek to FirstDay and you would get one. There is nothing like 53 week, AFAIK, so won't get that.

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        M Offline
        M Offline
        Maddie from Dartford
        wrote on last edited by
        #3

        Thanks d@ninsh, I think we can get that. Outlook calender displays that and that is what the help says for FirstFullWeek. If you use FirstFourDayWeek then it gives week 53 but i don't want to use because it will fail for me if new year week starts before wednesday.

        D 1 Reply Last reply
        0
        • M Maddie from Dartford

          Thanks d@ninsh, I think we can get that. Outlook calender displays that and that is what the help says for FirstFullWeek. If you use FirstFourDayWeek then it gives week 53 but i don't want to use because it will fail for me if new year week starts before wednesday.

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          If all you need to know is that first day is Monday or not, why not just check the DayOfWeek property for firstOfYear?

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          M 1 Reply Last reply
          0
          • M Maddie from Dartford

            Hi, I want to know the first week num of Jan 2010 if first day of week is Monday. All day before monday should be considered under last week of previous year, i.e. As first monday comes on 4th Jan 2010, i.e. I am expecting Jan 1,2 & 3 under week 53 of last year. I am using this code

            System.Globalization.CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentCulture;
            System.Globalization.Calendar calendar = currentCulture.Calendar;
            DateTime firstOfYear = new DateTime(DateTime.Now.Year, 1, 1);

            int weekNo = calendar.GetWeekOfYear(firstOfYear, System.Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);

            The week num is coming 52 instead of 53. What is going wrong? Is there any other way to do it? Thanks.

            modified on Thursday, January 7, 2010 10:19 AM

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            See my tip[^] to get the answer.

            M 1 Reply Last reply
            0
            • D dan sh

              If all you need to know is that first day is Monday or not, why not just check the DayOfWeek property for firstOfYear?

              50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

              M Offline
              M Offline
              Maddie from Dartford
              wrote on last edited by
              #6

              Thanks D@nish, I am not trying to know about monday as first day. I am trying to extract the week num of first week where first day of week for me is Monday. So, If the year (e.g. 2010) starts on Friday then Friday, Saturday & Sunday should be considered as days of last week of previous year and that is 53 for year 2009.I want this number(53). Week number 1 should start from 4 Jan 2010 (Monday).

              D D 2 Replies Last reply
              0
              • M Maddie from Dartford

                Thanks D@nish, I am not trying to know about monday as first day. I am trying to extract the week num of first week where first day of week for me is Monday. So, If the year (e.g. 2010) starts on Friday then Friday, Saturday & Sunday should be considered as days of last week of previous year and that is 53 for year 2009.I want this number(53). Week number 1 should start from 4 Jan 2010 (Monday).

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #7

                Oh. Once you get the day one first day of the month, you need to add/reduce the days to get to the first week accordingly. The tip posted in other reply would do that for you.

                50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                1 Reply Last reply
                0
                • L Lost User

                  See my tip[^] to get the answer.

                  M Offline
                  M Offline
                  Maddie from Dartford
                  wrote on last edited by
                  #8

                  Thanks Rich. Actually, I wanted to display week num for starting days before monday of new year.

                  L 1 Reply Last reply
                  0
                  • M Maddie from Dartford

                    Thanks Rich. Actually, I wanted to display week num for starting days before monday of new year.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    gajesh wrote:

                    Actually, I wanted to display week num for starting days before monday of new year.

                    OK, that requires a little bit of mathematics - using my sample week 1 of 2009 starts on Monday, December 29, 2008, so add 52 * 7 (364) and we get December 28 2009, which is beginning of week 53, since we know that week 1 of 2010 begins on 4th January.

                    M D 2 Replies Last reply
                    0
                    • L Lost User

                      gajesh wrote:

                      Actually, I wanted to display week num for starting days before monday of new year.

                      OK, that requires a little bit of mathematics - using my sample week 1 of 2009 starts on Monday, December 29, 2008, so add 52 * 7 (364) and we get December 28 2009, which is beginning of week 53, since we know that week 1 of 2010 begins on 4th January.

                      M Offline
                      M Offline
                      Maddie from Dartford
                      wrote on last edited by
                      #10

                      Thanks. Yeah.. I'll do this calulation. Thanks again.

                      1 Reply Last reply
                      0
                      • D dan sh

                        Change FirstFullWeek to FirstDay and you would get one. There is nothing like 53 week, AFAIK, so won't get that.

                        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        d@nish wrote:

                        There is nothing like 53 week, AFAIK, so won't get t

                        Oh yes there is! Week one of any year is the first week containing a Thursday in the new year. This means that Jan the first can be in week one, or in the last week of the previous year, which (depending on when Jan 1st was for that year) can be week 51, 52, or 53. There is an international standard for week of the year: ISO-8601[^] [edit]Typos due to typing with gloves on - it's cold in here![/edit]

                        All those who believe in psycho kinesis, raise my hand.

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        D 1 Reply Last reply
                        0
                        • M Maddie from Dartford

                          Thanks D@nish, I am not trying to know about monday as first day. I am trying to extract the week num of first week where first day of week for me is Monday. So, If the year (e.g. 2010) starts on Friday then Friday, Saturday & Sunday should be considered as days of last week of previous year and that is 53 for year 2009.I want this number(53). Week number 1 should start from 4 Jan 2010 (Monday).

                          D Offline
                          D Offline
                          dojohansen
                          wrote on last edited by
                          #12

                          Week 1 of 2009 started on Monday, January 5th. This means that December 31st 2009, which fell on Thursday, was week 52. Thus Friday January 1st 2010 was also week 52; weeks start on Mondays, not Fridays - or any other days. (EDIT: I rush to add that weeks DO of course start on other days in many systems, but they did not in this case! Just in an attempt to preempt those who may already be writing to point out my cultural insensitivity.) Thus you may *want* 53, but 52 is correct for 2010. The next time Jan 1st is in Week #53 is in 2013 (the last was in 2008). :)

                          modified on Friday, January 8, 2010 7:27 AM

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            d@nish wrote:

                            There is nothing like 53 week, AFAIK, so won't get t

                            Oh yes there is! Week one of any year is the first week containing a Thursday in the new year. This means that Jan the first can be in week one, or in the last week of the previous year, which (depending on when Jan 1st was for that year) can be week 51, 52, or 53. There is an international standard for week of the year: ISO-8601[^] [edit]Typos due to typing with gloves on - it's cold in here![/edit]

                            All those who believe in psycho kinesis, raise my hand.

                            D Offline
                            D Offline
                            dojohansen
                            wrote on last edited by
                            #13

                            Yes, but since weeks have 7 days and 52*7 = 364 and a (leap) year has 365 (366) days, this means the first day of the year can be week 1, week 52, or week 53. For example, if a year starts on a Friday, there are only three days belonging to that week in the new year. Thus week 1 might start on the fourth day of the year, in which case we'd still not be finished with week 52 on December 31st. The only times you get to week 53 is thus - if the week start on Monday - when a year starts on Monday, or on a Tuesday AND it is a leap year. For example, in a non-leap year that starts on Monday after 364 days or 52 full weeks it'll be Monday again but with one day to go; this will be Monday December 31st of whatever year it is, and the first day of week 53. The next day would then be January 1st, and since it's a Tuesday it clearly belongs to the same week as the Monday, since your weeks (as do mine) start on Monday.

                            1 Reply Last reply
                            0
                            • L Lost User

                              gajesh wrote:

                              Actually, I wanted to display week num for starting days before monday of new year.

                              OK, that requires a little bit of mathematics - using my sample week 1 of 2009 starts on Monday, December 29, 2008, so add 52 * 7 (364) and we get December 28 2009, which is beginning of week 53, since we know that week 1 of 2010 begins on 4th January.

                              D Offline
                              D Offline
                              dojohansen
                              wrote on last edited by
                              #14

                              If week one started December 29, 2009, I sure as heck wonder what week number you assign to the *previous* Monday, December 22, 2009. In fact, there was a week 53 in 2008, so week 1 in 2009 didn't get started until Monday January 5th. Thus the last day of 2009 was still in Week 52 and so were the first few days of 2010. Week 52 is therefore correct, and the original question was flawed. Not that this isn't understandable - it's very easy to put a foot wrong on these things. The complexity is quite moderate, but very subtle! (EDIT: By the way, I am fully aware that December 22, like December 29, did not fall on a Monday in 2009. The point is, and it's funny imo, there's been a rather humerously long week 53 or some unprecedented new all-time-high in week number if week one didn't get started until late December.)

                              L 1 Reply Last reply
                              0
                              • D dojohansen

                                If week one started December 29, 2009, I sure as heck wonder what week number you assign to the *previous* Monday, December 22, 2009. In fact, there was a week 53 in 2008, so week 1 in 2009 didn't get started until Monday January 5th. Thus the last day of 2009 was still in Week 52 and so were the first few days of 2010. Week 52 is therefore correct, and the original question was flawed. Not that this isn't understandable - it's very easy to put a foot wrong on these things. The complexity is quite moderate, but very subtle! (EDIT: By the way, I am fully aware that December 22, like December 29, did not fall on a Monday in 2009. The point is, and it's funny imo, there's been a rather humerously long week 53 or some unprecedented new all-time-high in week number if week one didn't get started until late December.)

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                I think you have got a bit confused, try re-reading my explanation.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups