计算特定年份的总周数 - ISO 8601 - VBA Access

时间:2015-12-04 14:22:32

标签: vba date ms-access iso8601

多看几次这个问题,但似乎无法在我的VBA代码中修复它。

我需要计算给定年份的总周数,符合ISO 8601。

当我使用datediff函数时:<div id="header"> <content id="headerText" select="[header]">111</content> </div> 它返回52而2015年有53周(ISO 8601)

我怎样才能完成这项工作?

4 个答案:

答案 0 :(得分:7)

再次引用https://en.wikipedia.org/wiki/ISO_8601#Week_dates

  12月28日总是在同一年的最后一周。

所以它真的像

一样简单
(function ($) {
  $.fn.extend({
    tableExport: function (options) {
      var defaults = {
        consoleLog: false,
        csvEnclosure: '"',
        csvSeparator: ',',
        csvUseBOM: true,
        displayTableName: false,
        escape: false,
        excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
        fileName: 'Whatever_You_Like',
        htmlContent: false,
        ignoreColumn: [],
        ignoreRow:[],
        jspdf: {orientation: 'p',

答案 1 :(得分:1)

DatePart,您之前是否尝试过这个

iNumWeeks = DatePart("ww", CDate("31/12/2015"), vbMonday, vbFirstFourDays) '53

enter image description here

答案 2 :(得分:0)

这里的简单功能将为任何一年提供正确的结果:

Public Function ISO_WeekCount( _
    ByVal datYear As Date) _
    As Byte

' Calculates number of weeks in year of datYear according to the ISO 8601:1988 standard.
'
' May be freely used and distributed.
' 2001-06-26. Gustav Brock, Cactus Data ApS, CPH

    Dim bytISO_Thursday As Byte

    ' No special error handling.
    On Error Resume Next

    bytISO_Thursday = Weekday(vbThursday, vbMonday)

    datYear = DateSerial(Year(datYear), 12, 31)
    ' Subtract one week if datYear is in week no. 1 of next year.
    datYear = DateAdd("ww", Weekday(datYear, vbMonday) < bytISO_Thursday, datYear)

    ISO_WeekCount = DatePart("ww", datYear, vbMonday, vbFirstFourDays)

End Function

答案 3 :(得分:-1)

如果您不介意使用UDF,则此代码将返回它 = ISOWeekNum(“2015年12月31日”) = ISOWeekNum(42369)返回53.

http://www.cpearson.com/excel/DateTimeVBA.htm

'http://www.cpearson.com/excel/DateTimeVBA.htm
'John Green, an Excel MVP from Australia.
Public Function ISOWeekNum(AnyDate As Date, Optional WhichFormat As Variant) As Integer
' WhichFormat: missing or <> 2 then returns week number,
'                                = 2 then YYWW
'
Dim ThisYear As Integer
Dim PreviousYearStart As Date
Dim ThisYearStart As Date
Dim NextYearStart As Date
Dim YearNum As Integer

ThisYear = Year(AnyDate)
ThisYearStart = YearStart(ThisYear)
PreviousYearStart = YearStart(ThisYear - 1)
NextYearStart = YearStart(ThisYear + 1)
Select Case AnyDate
    Case Is >= NextYearStart
        ISOWeekNum = (AnyDate - NextYearStart) \ 7 + 1
        YearNum = Year(AnyDate) + 1
    Case Is < ThisYearStart
        ISOWeekNum = (AnyDate - PreviousYearStart) \ 7 + 1
        YearNum = Year(AnyDate) - 1
    Case Else
        ISOWeekNum = (AnyDate - ThisYearStart) \ 7 + 1
        YearNum = Year(AnyDate)
End Select

If IsMissing(WhichFormat) Then Exit Function
If WhichFormat = 2 Then
    ISOWeekNum = CInt(Format(Right(YearNum, 2), "00") & _
    Format(ISOWeekNum, "00"))
End If

End Function

Public Function YearStart(WhichYear As Integer) As Date

Dim WeekDay As Integer
Dim NewYear As Date

NewYear = DateSerial(WhichYear, 1, 1)
WeekDay = (NewYear - 2) Mod 7 'Generate weekday index where Monday = 0

If WeekDay < 4 Then
    YearStart = NewYear - WeekDay
Else
    YearStart = NewYear - WeekDay + 7
End If

End Function

可以在查询中用作: SELECT DISTINCT dDate,ISOWeekNum(dDate)FROM tbl_MyTable