你如何确定 VBA 中的夏令时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/130877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you determine Daylight Savings Time in VBA?
提问by Lance Roberts
What function will let us know whether a date in VBA is in DST or not?
什么函数可以让我们知道 VBA 中的日期是否在 DST 中?
回答by Lance Roberts
For non-current dates (DST 2007+):
对于非当前日期(DST 2007+):
First, you need a function to find the number of specific weekdays in a month:
首先,您需要一个函数来查找一个月中特定工作日的数量:
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
Then, you can check for the DST day versus the following function calls:
然后,您可以检查 DST 日与以下函数调用:
Fall: NDow(Year(newdate), 11, 1, 1)
Spring: NDow(Year(newdate), 3, 2, 1)
秋季:NDow(Year(newdate), 11, 1, 1)
春季:NDow(Year(newdate), 3, 2, 1)
For the current date:
对于当前日期:
Call the Windows API function GetTimeZoneInformation, and it will return an enum (integer) with the status.
调用 Windows API 函数 GetTimeZoneInformation,它将返回一个带有状态的枚举(整数)。
I got the code for this from Chip Pearson's great Excel site.
我从 Chip Pearson 很棒的 Excel 网站上得到了这个代码。
回答by pursang
For anyone wondering how to account for daylight saving time in Europe (central europe time), I modified the script from Chip Pearson. The last sunday of March (2 AM to 3 AM) and October (3 AM to 2 AM) are the days when the hour switching occurs.
对于想知道如何考虑欧洲夏令时(中欧时间)的任何人,我修改了Chip Pearson的脚本。三月的最后一个星期日(凌晨 2 点到凌晨 3 点)和十月(凌晨 3 点到凌晨 2 点)是发生小时切换的日子。
Following code is the click event of a button in Excel:
以下代码是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
The module containing the necessary methods are to be found here.
可以在此处找到包含必要方法的模块。