VBA本地时区调整

标签 vba date excel timezone

假设我有一个本地太平洋时间 (UTC-8) Excel 值。例如,41656.672972014 年 1 月 17 日下午 4:09。我无法控制这个值。

我想相对于用户本地时间调整给定值。例如,都柏林的某人正在使用 Excel 工作簿。我想调整给定的数字并以他的本地时间 2014 年 1 月 18 日上午 12:09 显示它。有没有内置的 Excel 功能可以让这一切顺利进行?

最佳答案

VBA 本身不提供此选项,但有一个 Windows API 可以让您完成您想要的操作。 This article描述了如何执行此操作和 hereGetTimeZoneInformation 上的 MSDN 页面。

在 CPearson 页面中,首先进行以​​下声明:

Option Explicit

Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' NOTE: If you are using the Windows WinAPI Viewer Add-In to get
' function declarations, not that there is an error in the 
' TIME_ZONE_INFORMATION structure. It defines StandardName and
' DaylightName As 32. This is fine if you have an Option Base
' directive to set the lower bound of arrays to 1. However, if 
' your Option Base directive is set to 0 or you have no 
' Option Base diretive, the code won't work. Instead,
' change the (32) to (0 To 31).
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Type TIME_ZONE_INFORMATION
    Bias As Long
    StandardName(0 To 31) As Integer
    StandardDate As SYSTEMTIME
    StandardBias As Long
    DaylightName(0 TO 31) As Integer
    DaylightDate As SYSTEMTIME
    DaylightBias As Long
End Type


''''''''''''''''''''''''''''''''''''''''''''''
' These give symbolic names to the time zone
' values returned by GetTimeZoneInformation .
''''''''''''''''''''''''''''''''''''''''''''''

Private Enum TIME_ZONE
    TIME_ZONE_ID_INVALID = 0        ' Cannot determine DST
    TIME_ZONE_STANDARD = 1          ' Standard Time, not Daylight
    TIME_ZONE_DAYLIGHT = 2          ' Daylight Time, not Standard
End Enum


Private Declare Function GetTimeZoneInformation Lib "kernel32" _
    (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Private Declare Sub GetSystemTime Lib "kernel32" _
    (lpSystemTime As SYSTEMTIME)

然后这个函数将找到用户的本地时间和与 GMT 的偏移量:

Function LocalOffsetFromGMT(Optional AsHours As Boolean = False, _
    Optional AdjustForDST As Boolean = False) As Double
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' LocalOffsetFromGMT
' This returns the amount of time in minutes (if AsHours is omitted or
' false) or hours (if AsHours is True) that should be *added* to the
' local time to get GMT. If AdjustForDST is missing or false,
' the unmodified difference is returned. (e.g., Kansas City to London
' is 6 hours normally, 5 hours during DST. If AdjustForDST is False,
' the resultif 6 hours. If AdjustForDST is True, the result is 5 hours
' if DST is in effect.)
' Note that the return type of the function is a Double not a Long. This
' is to accomodate those few places in the world where the GMT offset 
' is not an even hour, such as Newfoundland, Canada, where the offset is
' on a half-hour displacement.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim TBias As Long
Dim TZI As TIME_ZONE_INFORMATION
Dim DST As TIME_ZONE
DST = GetTimeZoneInformation(TZI)

If DST = TIME_ZONE_DAYLIGHT Then
    If AdjustForDST = True Then
        TBias = TZI.Bias + TZI.DaylightBias
    Else
        TBias = TZI.Bias
    End If
Else
    TBias = TZI.Bias
End If
If AsHours = True Then
    TBias = TBias / 60
End If

LocalOffsetFromGMT = TBias

End Function

您可以使用以下方法测试代码:

MsgBox (LocalOffsetFromGMT())

然后要将此偏移量添加到原始时间,您可以执行以下操作:

OriginalTime + TIME(LocalOffsetFromGMT(True, True), 0, 0)

关于VBA本地时区调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21248506/

相关文章:

excel - 点击隐藏工作表?

ios - 如何获取最近 7 天的健康数据

javascript - 使用 Moment Timezone 格式化指定时区的日期

java - 从 mysql 获取日期

excel - Laravel 5.7 导入 Excel 文件 : Undefined offset

VBA查找空白单元格并插入文本

html - 如何使用 VBA 从网页中获取产品标题?

excel - 将一个范围从一个 VB 函数传递到另一个函数

vba - Excel vba : global variables are assigned when workbook is opened; get erased if an error occurs

php - Magento 发票 Excel 导出 - 如何更改字段?