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. How to get first and last date given a year and a weeknr

How to get first and last date given a year and a weeknr

Scheduled Pinned Locked Moved C#
tutorialquestion
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.
  • L Offline
    L Offline
    livez
    wrote on last edited by
    #1

    Hello! Im trying to figure out how I get the dates for the first and last day of a given year and week. For instance: year: 2009 week:53 = 2009/12/28 - 2010/01/03 year: 2010 week:1 = 2010/01/04 - 2010/01/10 Does anyone know how to achieve this?

    L A OriginalGriffO D 4 Replies Last reply
    0
    • L livez

      Hello! Im trying to figure out how I get the dates for the first and last day of a given year and week. For instance: year: 2009 week:53 = 2009/12/28 - 2010/01/03 year: 2010 week:1 = 2010/01/04 - 2010/01/10 Does anyone know how to achieve this?

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

      Use the DateTime structure[^] to create an object of the type you require, then adjust the values to get the dates you are looking for.

      L 1 Reply Last reply
      0
      • L Lost User

        Use the DateTime structure[^] to create an object of the type you require, then adjust the values to get the dates you are looking for.

        L Offline
        L Offline
        livez
        wrote on last edited by
        #3

        Hello and thanks for your reply. I don´t see how it would be that easy though. Could you give a pointer of how you would solve my problem with datetime-class? I´ve been trying to use System.Globalization.Calendar with AddWeeks etc, but have yet to make it work.

        L 1 Reply Last reply
        0
        • L livez

          Hello! Im trying to figure out how I get the dates for the first and last day of a given year and week. For instance: year: 2009 week:53 = 2009/12/28 - 2010/01/03 year: 2010 week:1 = 2010/01/04 - 2010/01/10 Does anyone know how to achieve this?

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          Try this - CultureInfo info = Thread.CurrentThread.CurrentCulture; DateTime dt = info.Calendar.AddWeeks(new DateTime(YYYY, MM, DD), x); where x is no of weeks. The interesting thing to note here is that if you use the first day of the first week of the previous year (i.e. 2008/12/28) evertything works fine. If you use new DateTime(2009, 1, 1),you would get that date plus 53 weeks which would not help you as per your query. Hope that makes sense :) .

          1 Reply Last reply
          0
          • L livez

            Hello! Im trying to figure out how I get the dates for the first and last day of a given year and week. For instance: year: 2009 week:53 = 2009/12/28 - 2010/01/03 year: 2010 week:1 = 2010/01/04 - 2010/01/10 Does anyone know how to achieve this?

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

            DateTime does not appear to handle week of year at all - much to my suprise. Fortunately, I had to implement this for a real-time project many years ago. For the full rules, look for ISO-8601 which is the standard reference for dates and times, but: 1) Week number is 1 - 53. 2) Weeks start on Monday. 3) Week one is always the week with the first Thursday of the new year. I.e., if January the 1st is a Monday, Tuesday, Wednesday or Thursday, it is in week 1. Otherwise it is in the final week of the previous year (which could be week 51, 52, or 53 dependant on when week 1 was in that year). Yes, this means that December 29th, 30th and 31st could be in Week 1 of the new year!

            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

            1 Reply Last reply
            0
            • L livez

              Hello and thanks for your reply. I don´t see how it would be that easy though. Could you give a pointer of how you would solve my problem with datetime-class? I´ve been trying to use System.Globalization.Calendar with AddWeeks etc, but have yet to make it work.

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

              livez wrote:

              Could you give a pointer of how you would solve my problem with datetime-class?

              Create a DateTime object with the relevant date, add or subtract one day until it's the first day of the week (i.e Sunday or Monday), save that date. Add one day until it's the last day of the week, save that date. Look at the DateTime members to see what is possible.

              L 1 Reply Last reply
              0
              • L livez

                Hello! Im trying to figure out how I get the dates for the first and last day of a given year and week. For instance: year: 2009 week:53 = 2009/12/28 - 2010/01/03 year: 2010 week:1 = 2010/01/04 - 2010/01/10 Does anyone know how to achieve this?

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                This might help: http://en.wikipedia.org/wiki/ISO_week_date[^] It tells you how to calculate the week number for any given date, so it shouldn't be too hard to "reverse engineer" it to come up with the date corresponding to a given week number.

                L 1 Reply Last reply
                0
                • D David Skelly

                  This might help: http://en.wikipedia.org/wiki/ISO_week_date[^] It tells you how to calculate the week number for any given date, so it shouldn't be too hard to "reverse engineer" it to come up with the date corresponding to a given week number.

                  L Offline
                  L Offline
                  livez
                  wrote on last edited by
                  #8

                  Thanks, Ill check it out!

                  L 1 Reply Last reply
                  0
                  • L livez

                    Thanks, Ill check it out!

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

                    I already got my code to work.

                    D 1 Reply Last reply
                    0
                    • L Lost User

                      I already got my code to work.

                      D Offline
                      D Offline
                      David Skelly
                      wrote on last edited by
                      #10

                      Richard MacCutchan wrote:

                      I already got my code to work.

                      Teacher's pet! ;P

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        livez wrote:

                        Could you give a pointer of how you would solve my problem with datetime-class?

                        Create a DateTime object with the relevant date, add or subtract one day until it's the first day of the week (i.e Sunday or Monday), save that date. Add one day until it's the last day of the week, save that date. Look at the DateTime members to see what is possible.

                        L Offline
                        L Offline
                        livez
                        wrote on last edited by
                        #11

                        "Create a DateTime object with the relevant date" But its the relevant date I dont have. I have a year and a weeknr. How do I create a relevant datetime-object with that?

                        L 1 Reply Last reply
                        0
                        • L livez

                          "Create a DateTime object with the relevant date" But its the relevant date I dont have. I have a year and a weeknr. How do I create a relevant datetime-object with that?

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

                          Create a DateTime(year, 1, 1) Adjust by day until the day is Thursday to get it to week 1 Add 7 x week number Adjust day back and forward to find Monday and Sunday

                          L 2 Replies Last reply
                          0
                          • D David Skelly

                            Richard MacCutchan wrote:

                            I already got my code to work.

                            Teacher's pet! ;P

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

                            Yeah and I brought an apple today. ;)

                            1 Reply Last reply
                            0
                            • L Lost User

                              Create a DateTime(year, 1, 1) Adjust by day until the day is Thursday to get it to week 1 Add 7 x week number Adjust day back and forward to find Monday and Sunday

                              L Offline
                              L Offline
                              livez
                              wrote on last edited by
                              #14

                              Ill try that, thanks!

                              1 Reply Last reply
                              0
                              • L Lost User

                                Create a DateTime(year, 1, 1) Adjust by day until the day is Thursday to get it to week 1 Add 7 x week number Adjust day back and forward to find Monday and Sunday

                                L Offline
                                L Offline
                                livez
                                wrote on last edited by
                                #15

                                got it working with your suggestion (with a slight adjustment), thanks!

                                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