VBA的日期节省时间的两个日期之间的小时数差异

发布于 2025-01-17 10:54:40 字数 101 浏览 2 评论 0原文

由于节目节省了时间的变化,上周日有23个小时。结果,我的一些日常计算是不正确的,因为它们是用24小时的假设制成的。

我找不到计算这23小时的方法(在冬季变化的情况下为25)。

Last Sunday had 23 hours due to the daylight saving time change. As a result some of my daily calculations are incorrect because they are made with an assumption of 24 hours.

I cannot find the method to calculate those 23 hours (25 in the case of the winter time change).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2025-01-24 10:54:40

您可以在我的项目中使用时区函数biaSwindowstimezone github

vba.timezone-windows

(Module wtzicore)以获取开始时开始的偏差值和最终时间。然后,将偏差值添加到日期值并计算小时差:

Public Function HoursNormal( _
    ByVal Date1 As Date, _
    ByVal Date2 As Date, _
    ByVal Timezone As String) _
    As Long

    Dim Date1Normal As Date
    Dim Date2Normal As Date
    Dim Hours       As Long
    
    Date1Normal = DateAdd("n", BiasWindowsTimezone(Timezone, True, Date1), Date1)
    Date2Normal = DateAdd("n", BiasWindowsTimezone(Timezone, True, Date2), Date2)
    
    Hours = DateDiff("h", Date1Normal, Date2Normal)
    
    HoursNormal = Hours

End Function

时区必须是您的字面意义,如 Windows注册表,例如w。欧洲(还可以检索并列出这些代码),使用您的周日日期,您将获得23小时:

Date1 = #2022-03-27 00:00#
Date2 = #2022-03-28 00:00#

? HoursNormal(Date1, Date2, "W. Europe")
 23 

You can use the timezone function BiasWindowsTimezone in my project at GitHub:

VBA.Timezone-Windows

(module WtziCore) to obtain the bias values for the start and the end time taking DST in account. Then, add the bias values to both date values and calculate the difference in hours:

Public Function HoursNormal( _
    ByVal Date1 As Date, _
    ByVal Date2 As Date, _
    ByVal Timezone As String) _
    As Long

    Dim Date1Normal As Date
    Dim Date2Normal As Date
    Dim Hours       As Long
    
    Date1Normal = DateAdd("n", BiasWindowsTimezone(Timezone, True, Date1), Date1)
    Date2Normal = DateAdd("n", BiasWindowsTimezone(Timezone, True, Date2), Date2)
    
    Hours = DateDiff("h", Date1Normal, Date2Normal)
    
    HoursNormal = Hours

End Function

The timezone must be the literal of yours as found in the Windows Registry, for example W. Europe (there is code as well to retrieve and list these), and using your Sunday date, you will get 23 hours:

Date1 = #2022-03-27 00:00#
Date2 = #2022-03-28 00:00#

? HoursNormal(Date1, Date2, "W. Europe")
 23 
微暖i 2025-01-24 10:54:40

这将在3月27日返回23小时,并在10月30日的时间变化中返回25小时。

Function HorasDia(datFecha As Date) As Byte
Dim OutlookApp As Object
On Error Resume Next

Set OutlookApp = CreateObject("Outlook.Application")
HorasDia = DateDiff("h", OutlookApp.TimeZones.ConvertTime(datFecha, OutlookApp.TimeZones.Item("W. Europe Standard Time"), OutlookApp.TimeZones.Item("UTC")), OutlookApp.TimeZones.ConvertTime(datFecha + 1, OutlookApp.TimeZones.Item("W. Europe Standard Time"), OutlookApp.TimeZones.Item("UTC")))

If Not OutlookApp Is Nothing Then Set OutlookApp = Nothing
End Function

This returns 23h for March 27th and 25h for Oct 30th dates of the time change.

Function HorasDia(datFecha As Date) As Byte
Dim OutlookApp As Object
On Error Resume Next

Set OutlookApp = CreateObject("Outlook.Application")
HorasDia = DateDiff("h", OutlookApp.TimeZones.ConvertTime(datFecha, OutlookApp.TimeZones.Item("W. Europe Standard Time"), OutlookApp.TimeZones.Item("UTC")), OutlookApp.TimeZones.ConvertTime(datFecha + 1, OutlookApp.TimeZones.Item("W. Europe Standard Time"), OutlookApp.TimeZones.Item("UTC")))

If Not OutlookApp Is Nothing Then Set OutlookApp = Nothing
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文