2010-01-06 2 views
0

주어진 연도의 주 수를 반환하는 함수를 수정하려고합니다. 여기날짜로 주 번호를 계산하십시오.

가 보이는 방법은 다음과 같습니다

이 기능은 날짜 2010년 12월 31일이 부여됩니다
Function GetWeekNo(date) 
    weekOfYear = DatePart("ww", DateValue(date), vbMonday, vbFirstFourDays) 

    If weekOfYear > 52 Then 
     If DatePart("ww", DateValue(date) + 7, vbMonday, vbFirstFourDays) = 2 Then 
      weekOfYear = 1 
     End If 
    End If 

    GetWeekNo = weekOfYear 
End Function 

는 2010 년

주 53 주 (52)가있다 반환 : 나는 어떤이 없습니다 고전적인 ASP 경험.

답변

4

어떤 주를 "올해의 첫 주"로 간주하는지에 따라 다릅니다.

DatePart("ww", "12/31/2010", vbMonday) 
' returns 53 
' FirstWeekOfYear parameter defaults to vbFirstJan1 
' the week that contains January/01/2010 
' here, its the week starting on December/28/2009 

DatePart("ww", "12/31/2010", vbMonday, vbFirstFourDays) 
' returns 52 
' FirstWeekOfYear parameter set to vbFirstFourDays 
' the first week that has at least four days of the new year 
' here, its the week starting on January/04/2010 

DatePart("ww", "12/31/2010", vbMonday, vbFirstFullWeek) 
' returns 52 
' FirstWeekOfYear parameter set to vbFirstFullWeek 
' the first week that has full seven days of the new year 
' here, again, its the week starting on January/04/2010 

올해의 첫 주에 대한 정의를 결정한 다음 그에 따라 DatePart 기능을 사용하십시오.

+0

의미가 있습니다. 이것은 모든 문화권에 적용됩니까? – roosteronacid

+0

아니요, 반드시 그런 것은 아닙니다. 대부분의 사람들에게는 예외가 있지만 기본값 인 vbFirstJan1을 사용하는 것이 좋습니다. 예를 들어 많은 사람들이 주 수를 52로 예상합니다. –