README.md

December 5, 2022 ยท View on GitHub

Get WeekNumber for a given date

Description

This snippet will allow you to pass a string as a date, and get the week number for that calendar year, returned as an integer

More Info

Date, passed as a string

Week number in calendar year, returned as an integer

Submitted On
ByJeff Mayes
LevelBeginner
User Rating4.7 (14 globes from 3 users)
CompatibilityVB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
CategoryMath/ Dates
WorldVisual Basic
Archive File

Source Code

Public Function WeekNum(sDate As String) As Integer
Dim iYear As Integer
Dim iMon As Integer
Dim sTemp As String
Dim iDay11 As Integer
Dim iDiff As Integer
  If IsDate(sDate) And sDate <> "12:00:00 PM" Then
      'determine the year
    iYear = Year(sDate)
      'determine the month
    iMon = Month(sDate)
      'determine weekday of Jan 1st
    sTemp = "1/1/" & Trim(Str$(iYear))
    iDay11 = Weekday(sTemp)
      'now calculate the difference in days
    iDiff = DateDiff("d", sTemp, sDate)
      'base week
    WeekNum = Int(iDiff / 7) + 1
      'check for rollover based on day of week
    If Weekday(sDate) < iDay11 Then
      WeekNum = WeekNum + 1
    End If
  End If
End Function