代码之家  ›  专栏  ›  技术社区  ›  Lance Roberts

您如何确定VBA中的夏令时?

  •  3
  • Lance Roberts  · 技术社区  · 16 年前

    什么函数可以让我们知道vba中的日期是否为dst?

    2 回复  |  直到 9 年前
        1
  •  5
  •   Community CDub    7 年前

    对于非当前日期(DST 2007+):

    首先,您需要一个函数来查找一个月内的特定工作日数:

    Public Function NDow(Y As Integer, M As Integer, _
                    N As Integer, DOW As Integer) As Date  
    
    ' Returns Date of Nth Day of the Week in Month  
    
    NDow = DateSerial(Y, M, (8 - Weekday(DateSerial(Y, M, 1), _
                  (DOW + 1) Mod 8)) + ((N - 1) * 7))  
    
    End Function  
    

    然后,您可以对照以下函数调用检查DST日期:

    秋季:NDOW(年份(新日期),11,1,1)
    春季:NDOW(年(新日期)、3、2、1)

    对于当前日期:

    调用Windows API函数getTimeZoneInformation, 它将返回一个状态为的枚举(整数)。

    我从Chip Pearson伟大的Excel网站上得到了这个代码。

    Pearson's site

        2
  •  4
  •   Community CDub    7 年前

    对于任何想知道如何计算欧洲夏令时(中欧时间)的人,我修改了脚本 Chip Pearson . 三月的最后一个星期天(凌晨2点到凌晨3点)和十月(凌晨3点到凌晨2点)是小时转换发生的日子。

    以下代码是Excel中按钮的单击事件:

    Dim dates As String
    dates = "A1:A20"
    
    For Each c In Worksheets("Sheet1").Range(dates).Cells
        If (IsDateWithinDST(c.Value)) Then
            c.Value = DateAdd("h", 1, c.Value)
        End If
    Next
    

    要找到包含必要方法的模块 here .

    More info on DST in Europe.