当前位置: 首页 > 知识库问答 >
问题:

访问VBA:将ISO周号转换为日期范围时出错

齐威
2023-03-14

我试图创建一个基于ISO周号的简单周选择器,它将给我周一日期和周日日期每次用户点击“当前周”或“前一周”或“下周”按钮,因为我将选择这些日期内的所有交易。

希望你们能帮帮我。

提前谢谢你。

'''' This is the function in a module to get the week number

Public Function ISOWeek(MyDate As Date) As Integer

    ISOWeek = Format(MyDate, "ww", vbMonday, vbFirstFourDays)
    
    If ISOWeek > 52 Then
    
        If Format(MyDate + 7, "ww", vbMonday, vbFirstFourDays) = 2 Then ISOWeek = 1
        
    End If

End Function


'''' These subs run on the form code

Private Sub NextWeek_Click()

    Dim SelectedWeek As Date

    SelectedWeek = Me.Date_From.Value

    FirstDayWeek = DateAdd("ww", ISOWeek(SelectedWeek), DateSerial(Year(SelectedWeek), 1, 1) - 2)

    LastDayWeek = DateAdd("ww", ISOWeek(SelectedWeek), DateSerial(Year(SelectedWeek), 1, 1) + 4)

    Me.Date_From.Value = FirstDayWeek
    Me.Date_To.Value = LastDayWeek

End Sub

Private Sub PreviousWeek_Click()

    Dim SelectedWeek As Date

    SelectedWeek = Me.Date_From.Value
    
    FirstDayWeek = DateAdd("ww", ISOWeek(SelectedWeek) - 2, DateSerial(Year(SelectedWeek), 1, 1) - 2)

    LastDayWeek = DateAdd("ww", ISOWeek(SelectedWeek) - 2, DateSerial(Year(SelectedWeek), 1, 1) + 4)

    Me.Date_From.Value = FirstDayWeek
    Me.Date_To.Value = LastDayWeek

End Sub

共有1个答案

穆展鹏
2023-03-14

从日期计算中保留周数,它们只会使事情复杂化。

通过使用下面列出的泛型函数,您的两个函数可以简化为:

Private Sub NextWeek_Click()

    Me.Date_From.Value = DateNextWeekPrimo(Me.Date_From.Value, vbMonday)
    Me.Date_To.Value = DateNextWeekUltimo(Me.Date_From.Value, vbMonday)

End Sub

Private Sub PreviousWeek_Click()

    Me.Date_From.Value = DatePreviousWeekPrimo(Me.Date_From.Value, vbMonday)
    Me.Date_To.Value = DatePreviousWeekUltimo(Me.Date_From.Value, vbMonday)

End Sub


' Returns the primo date of the week following the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateNextWeekPrimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = 1
    Interval = "ww"
    
    ' Offset date.
    ResultDate = DateAdd(Interval, Number, DateThisWeek)
    
    ' Return first weekday with no time part.
    ResultDate = DateAdd("d", 1 - Weekday(ResultDate, FirstDayOfWeek), Fix(ResultDate))
    
    DateNextWeekPrimo = ResultDate
    
End Function


' Returns the ultimo date of the week following the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateNextWeekUltimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = 1
    Interval = "ww"
    
    ' Offset date.
    ResultDate = DateAdd(Interval, Number, DateThisWeek)

    ' Return last weekday with no time part.
    ResultDate = DateAdd("d", 7 - Weekday(ResultDate, FirstDayOfWeek), Fix(ResultDate))
    
    DateNextWeekUltimo = ResultDate
    
End Function


' Returns the primo date of the week preceding the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DatePreviousWeekPrimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = -1
    Interval = "ww"
    
    ' Offset date.
    ResultDate = DateAdd(Interval, Number, DateThisWeek)
    
    ' Return first weekday with no time part.
    ResultDate = DateAdd("d", 1 - Weekday(ResultDate, FirstDayOfWeek), Fix(ResultDate))
    
    DatePreviousWeekPrimo = ResultDate
    
End Function


' Returns the ultimo date of the week preceding the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DatePreviousWeekUltimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = -1
    Interval = "ww"
    
    ' Offset date.
    ResultDate = DateAdd(Interval, Number, DateThisWeek)

    ' Return last weekday with no time part.
    ResultDate = DateAdd("d", 7 - Weekday(ResultDate, FirstDayOfWeek), Fix(ResultDate))
    
    DatePreviousWeekUltimo = ResultDate
    
End Function
 类似资料:
  • 问题内容: 给定一年和一个日历周,我如何获得该周的星期二作为日期? 问题答案: 给定您和(日历周)作为变量(例如,从SELECT语句中获取),您可以通过以下方式获取DATE: 该短语重复;不想存储变量。SQL语句在MySQL上对我来说效果很好。 更新: 只是为了澄清:将产生一周的第一天。从中减去一个数字(星期二为-1;星期三为-2,依此类推,将为您选择一周中的特定日期)。看这里。

  • 问题内容: 通过这个简单的功能,我可以获得星期几。现在,用星期几,如何获取从星期日开始的日期范围? 欢迎任何帮助。谢谢。 问题答案: 前言: 返回您从星期一开始的星期数,因此我将回答您的问题,该问题也处理从星期一开始的星期。如果您希望它在周日开始的几周内工作,请根据需要进行更改。 我在中发布了该实用程序,请参见。 标准库没有提供可以返回给定星期(年+周数)的日期范围的函数。因此,我们必须自己构建。

  • 问题内容: 如何将时间转换为ISO 8601日期格式?(-_-;) 问题答案: 面向对象 这是推荐的方法。 程序 对于旧版本的PHP,或者您更熟悉过程代码。

  • 我想把两次加在一起。国际标准化组织 有什么建议吗?

  • 我需要将一些字符串日期转换为另一种格式的字符串日期 我想按照“yyyy-MM-dd'T'HH:MM:ss.SSSXX”模式获得ISO-8601格式的结果日期字符串 你能帮我实现这个吗?

  • 我有一个ISO 8601日期字符串,格式如下: 我需要使用日期字符串作为我在BigQuery中运行的查询的一部分。我正在尝试使用<code>com.google.cloud.bigquery。QueryParameterValue类将其转换为类型为的,如下所示: 这给了我一个错误: Eclipse中时间戳方法的内联帮助声明: 如何将 转换为所需的格式?有没有更好的方法可以使用来处理从ISO 860