![]() ![]() Public Function GetPrev(d As DateTime, day As DayOfWeek) As DateTime ![]() 'Get the previous date with the supplied DoW Return New Date(year, 1, 1, Me).AddDays(ordinal - 1) Public Function GetDate(year As Integer, week As Integer, Optional day As DayOfWeek = DayOfWeek.Monday) As DateTimeĭim ordinal = (week * 7) + IsoDayOfWeek(day) - (GetIsoDayOfWeek(New Date(year, 1, 4, Me)) + 3) 'Get the date from the year, week and DoW, again, using calculation from Public Function GetYearAndWeek(d As DateTime) As Tuple(Of Integer, Integer)ĭim week As Integer = Math.Floor((Me.GetDayOfYear(d) - dow + 10) / 7) Public Sub GetYearAndWeek(d As DateTime, Optional ByRef Year As Integer = 0, Optional ByRef Week As Integer = 0) Public Function GetWeekYear(d As DateTime) As Integer Public Overloads Function GetWeekOfYear(d As DateTime) As Integer I'm not sure about VSExpress, but in full VB, I'd just extend GregorianCalendar to add functionality to get the week year and simplify access to the correct week number: Public Class IsoWeekCalendar However, I do think the following class is a nice wrapper for the method explained above, so I modified it to incorporate the calculation method I used there. I just noticed the last response to this thread was also made by myself, talk about reinventing the wheel. 'it's not, this week is also in the next year 'determine if this week's thursday is in this year, if OrElse jan1st.DayOfWeek = DayOfWeek.Wednesday _ If (jan1st.DayOfWeek = DayOfWeek.Thursday _ 'or is a leap year starting on a wednesday 'A week has 53 weeks only if it starts on a thursday 'if we get week=0, the week is actually in last year 'If the week is 0 or 53, we should do some checks 'usually, this calculation gives the week numberĭim week = Math.Floor((g.GetDayOfYear(day) - dow + 10) / 7) 'Per MSDN, dow is 0 for sunday, per ISO, this should be 7 I don't know about VS Express, but I just wrote this (in full VS) based on the wikipedia article: Public Function GetIsoYearAndWeek(day As Date) As Integer()ĭim g As New () Keeping my question present should help others in their search for the same. I will not mark my question as a duplicate, because their question title "Calculate the start and end date of a week given the week number and year in C# (based on the ISO specification)" is distinctly different from mine and they mention C# (not Visual Basic although they do want Visual Basic as well). That follows the ISO specification.Įxample: int week = m圜alendar.GetWeekOfYear(DateTime.Today,ĬalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) How the weeks are determined, and DayOfWeek.Monday to specify theįirst weekday. Of a date, with the CalendarWeekRule.FirstFourDayWeek value to specify You can use the Calendar.GetWeekOfYear method to get the week number Note to use this solution I should add that you need to import the Calendar abstract class defintion: Imports System.GlobalizationĪND then instantiate an Instance of Calendar, I chose Gregorian Calendar as it is the defacto standard, but there are other (cultural) options: Dim m圜alendar As Calendar = New GregorianCalendar Thanks - an answer in that question is pretty close, if not exactly what I'm looking for: ![]() Other routines: But doesn't state how it is based on the ISO standardĪlso, a search on Stack Overflow gave me results that weren't quite what I was looking for: (has this been solved? It's rather old, 2004 so perhaps?) "BUG: Format or DatePart Functions Can Return Wrong Week Number for Last Monday in Year": I found some routines that were Visual Basic for Applications oriented reliant on Excel environment, some things not present in Visual Studio Express 2010 Visual Basic:ĭatePart Visual Basic library function may be useful: (v=vs.80).aspx - BUT apparently there is a bug: to run in a Visual Studio Express 2010 Visual Basic project) certified as being correct or tested reasonably thoroughly) for calculating the ISO week from a date, in Visual Basic (i.e. I'm looking for a definitive, standard (i.e. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |