How to get first and last date given a year and a weeknr
-
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.
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.
-
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?
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 :) .
-
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?
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.
-
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.
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.
-
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?
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.
-
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.
-
Richard MacCutchan wrote:
I already got my code to work.
Teacher's pet! ;P
-
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.
-
"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?
-
Richard MacCutchan wrote:
I already got my code to work.
Teacher's pet! ;P
-
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
-
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