-1

I have tried searching for a solution which gives the correct week number for the date value.
link1, link2,link3
Followed the methods in the above links, but for the date 30/12/2014, I get the week number as 53. but it falls as 1st week of 2015 year.
I tried the below methods to get the week number of the year for the specific date.

private int GetWeekNumberOfTheYear() {
var currentCulture = CultureInfo.CurrentCulture;
// option 1 
var weekNo = currentCulture.Calendar.GetWeekOfYear(DateTime.Now,currentCulture.DateTimeFormat.CalendarWeekRule, currentCulture.DateTimeFormat.FirstDayOfWeek);
// option 2 
var weekNo = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
return weekNo; }

Is the method above is correct to return 53 as week number or it should be 1 ?
Is there any mistake in the above code. Suggestions please. EDIT :

Found many searches specified, Dec 29th 2014 to 4th Jan 2015 as 1st week of year 2015.
So my confusion is the present week must be taken as 53rd Week or 1st Week.

http://week-number.net/calendar-with-week-numbers-2014.html
http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php

Community
  • 1
  • 1
kk1076
  • 1,740
  • 13
  • 45
  • 76
  • 3
    Why would December 30th *ever* be the first week of the year? – Jeroen Vannevel Dec 30 '14 at 17:49
  • There are 52.1775 weeks in a year, so in other words either one or two days in a 53rd week. Therefore it is, of course, correct to return 53. Think...why would the 30th day of December(has 31 days) be in the 1st week of 2015. – Adam Dec 30 '14 at 17:50
  • 1
    @JeroenVannevel I guess his confusion comes from Dec 30th falling on the same week as Jan 1st. – Cory Nelson Dec 30 '14 at 17:51
  • So do you definitely want the ISO-8601 week-of-week-year? If so, Noda Time is your way forward. It would be worth clarifying the question though. "Week number" is an ambiguous concept. – Jon Skeet Dec 30 '14 at 17:51
  • Thanks for your suggestions. But when I searched for the week number of the year 2014, got Dec 29th 2014 to Jan 4th 2015 as 1st week number of year 2015. reference : http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php – kk1076 Dec 30 '14 at 17:54
  • 2
    Yes, that's the ISO-8601 week year. (As documented at http://www.epochconverter.com/epoch/weeknumbers.php) - is that definitely what you want? – Jon Skeet Dec 30 '14 at 17:56
  • There are several different ways to define week numbers depending on culture so to answer your question you have to specify which week numbering rule you want to use. In some years week numbers are off by one when you travel from one country to another and at least the ISO rule allow the last days of the year to be in week 1 and the first days of the year to be in week 53. – Martin Liversage Dec 30 '14 at 21:40

1 Answers1

6

If you're looking for the ISO-8601 week-of-week-year, you could use my Noda Time project:

var date = new LocalDate(2014, 12, 30);
var week = date.WeekOfWeekYear; // 1
var weekYear = date.WeekYear; // 2015

You can get a LocalDate from a DateTime via a LocalDateTime, but ideally you'd use the Noda Time times as widely as possible through your project. (That's the way I'd hope you'd get the maximum benefit, anyway.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194