![]() ![]() Which allows us to: document.writeln(d.Version for the United Kingdom with UK bank holidays. However, I prefer to extend the date object with the method instead. You can use it like this: var d = new Date() Calculate full weeks to nearest Thursday Var yearStart = new Date(d.getFullYear(), 0, 1) Set to nearest Thursday: current date + 4 - current day numberĭ.setDate(d.getDate() + 4 - (d.getDay() || 7)) * Note that dates in one year can be weeks of previous * between that date and the first day of that year. * Algorithm is to find nearest thursday, it's year * For a given date, get the ISO week number There are several scripts out there which can do that. Neither JavaScript nor Globalize has a method for getting the week number. If you try January the 6th it will show week 1, and January the 7th will show week 2. That’s the only exception that I’ve found. The result follows: Figure 5: ISO week number Var week = calendar.GetWeekOfYear(theDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) Var theDate = new DateTime(2013, 12, 31) Let’s prove that with some code: using System.Globalization However, if we examine the calendar in Windows it will show the following for January 2013: Figure 4: Week bugīy the ISO definition, the 31st of December should be part of week 1, 2013. The output follows: Figure 3: Different week numbers for first week of year. Week = calendar.GetWeekOfYear(theDate, formatRules.CalendarWeekRule, formatRules.FirstDayOfWeek) = new CultureInfo("en-US") Ĭalendar = įormatRules = Var week = calendar.GetWeekOfYear(theDate, formatRules.CalendarWeekRule, formatRules.FirstDayOfWeek) To get the week of the year you simply invoke: using System.Globalization In the United States, (amongst several countries) the week starts with Sunday, and the first week of the year begins with January 1st. But that’s if you follow the ISO specification as shown in the Swedish calendar below. The first week of 2013 started with 31st of December this year, while it started with the 2nd of January the year before. The first week of the year is either calculated by finding the first of January or the week including January 4th. Week numbers are complicated, since there are different ways to calculate which week it is. ![]()
0 Comments
Leave a Reply. |