find week num of first week in year [modified]
-
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
-
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
-
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...!!
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.
-
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.
-
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
-
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...!!
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).
-
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).
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...!!
-
Thanks Rich. Actually, I wanted to display week num for starting days before monday of new year.
-
Thanks Rich. Actually, I wanted to display week num for starting days before monday of new year.
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.
-
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.
Thanks. Yeah.. I'll do this calulation. Thanks again.
-
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...!!
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.
-
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).
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
-
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.
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.
-
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.
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.)
-
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.)