In vb we can get the week number counting from January. ex Jan 1st is Week 1 and Feb 1st is Week 5 etc by .DatePart(DateInterval.WeekOfYear, Now)
I need to count the week number from a given date. ex: if set the base to July 1st then July 1st is the week 1 and based on that what would be the week number for Oct 3rd? How can I do this in VB?
You can use Calendar.GetWeekOfYear
.
Here's an example
Dim dateNow = DateTime.Now
Dim dfi = DateTimeFormatInfo.CurrentInfo
Dim calendar = dfi.Calendar
' using Thursday because I can.
Dim weekOfyear = calendar.GetWeekOfYear(
dateNow, _
dfi.CalendarWeekRule, _
DayOfWeek.Thursday)
Something like this should do the trick:
Private Function GetWeekNumber(ByVal startMonth As Integer, ByVal startDay As Integer, ByVal endDate As Date) As Integer
Dim span As TimeSpan = endDate - New Date(endDate.Year, startMonth, startDay)
Return (CInt(span.TotalDays) \ 7) + 1
End Function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With