Advertisement
5_2007-2008 Math/ Dates #179868

Get WeekNumber for a given date

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

AI

AI 요약: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

소스 코드
original-source
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
원본 댓글 (3)
Wayback Machine에서 복구됨