首页 > 解决方案 > 使用 Windows api TimeZoneInfo ConvertTimeFromUtc

问题描述

在powershell中我可以使用windows系统api

$estzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")
$esttime = [System.TimeZoneInfo]::ConvertTimeFromUtc($test2, $estzone)
"GMT= " +$test2 + " EST =" + $esttime

我似乎无法在 VB 中引用 FindSystemTimeZoneById 或 ConvertTimeFromUtc

我努力了

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

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

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 PtrSafe Function FindSystemTimeZoneById Lib "kernel32" (lpTimeZone As String) As TIME_ZONE_INFORMATION
Private Declare PtrSafe Function ConvertTimeFromUtc Lib "kernel32" (lpSystemTime As Date, pTimeZoneInformation As TIME_ZONE_INFORMATION) As Long


Function GetLocalTimeFromGMTerc(GMTTime As Date)
    Dim estzone As TIME_ZONE_INFORMATION
    Dim esttime As Date
    estzone = FindSystemTimeZoneById("Eastern Standard Time")
    esttime = ConvertTimeFromUtc(GMTTime, estzone)
    GetLocalTimeFromGMTerc = esttime
End Function

这似乎可以编译/运行,但给出“公式中使用的值是错误的数据类型”我认为我做错了。有什么帮助吗?

谢谢!

标签: excelvba

解决方案


推荐阅读