1

Following code for getting weekid

 CultureInfo ciCurr = CultureInfo.CurrentCulture;
  int Weekid = ciCurr.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

When I try for date 31/12/2018 it showed me week Id 53 but in my DatePicker it showed week Id 1

Refer image of DatePicker

enter image description here

Question:

which one is correct?

how to make it same either change in C# code or jquery code ?

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
User
  • 1,334
  • 5
  • 29
  • 61
  • 1
    Both are correct, notice the `CalendarWeekRule.FirstFourDayWeek` parameter in C# that specifies HOW to determine first week of the year. As of what is jQuery using and how to change it - I don't know. – orhtej2 Jul 16 '18 at 07:43
  • *CalendarWeekRule* I tried to change in rules but output provide me same. any other way – User Jul 16 '18 at 07:45
  • @Pravin what jQuery is doing appears to be `CalendarWeekRule.FirstDay`. – orhtej2 Jul 16 '18 at 07:48

1 Answers1

2

Try this code..

Source: https://stackoverflow.com/a/11154921/1997983

 var d = new DateTime(2018, 12, 31);
            CultureInfo cul = CultureInfo.CurrentCulture;

            var firstDayWeek = cul.Calendar.GetWeekOfYear(
                d,
                CalendarWeekRule.FirstDay,
                DayOfWeek.Monday);

            int weekNum = cul.Calendar.GetWeekOfYear(
                d,
                CalendarWeekRule.FirstDay,
                DayOfWeek.Monday);

            int year = weekNum == 52 && d.Month == 1 ? d.Year - 1 : d.Year;
            Console.WriteLine("Year: {0} Week: {1}", year, weekNum);

It is returning 1 for week 53 as per ISO-8601

http://blogs.msdn.com/b/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net.aspx

If you check the week of 31 Dec 2018, it will start from 30 Dec which falls on Saturday. From this year-2018 it will contain only two dates.. 30 and 31. Rest all 5 dates are from next year i.e. 2019. Hence it is returning 1 as the week number which is correct as per ISO-8601 standard.

Rakesh Burbure
  • 1,045
  • 12
  • 27