小智 12
简单地说,您可以使用COM对象来实现UTC时间信息.
Dim dt As Object, utc As Date
Set dt = CreateObject("WbemScripting.SWbemDateTime")
dt.SetVarDate Now
utc = dt.GetVarDate(False)
Run Code Online (Sandbox Code Playgroud)
bra*_*man 11
http://excel.tips.net/Pages/T002185_Automatically_Converting_to_GMT.html
该页面上有一个带有LocalTimeToUTC方法的宏.看起来它会成功.还有一些公式示例,如果你想走这条路线.
编辑 - 另一个链接. http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx 此页面有几种日期/时间方法.选择你的毒药.要么应该这样做,但我觉得第二个更漂亮.;)
虽然这个问题很老,但我只是花了一些时间将基于此的清洁代码整理在一起,我想在这里发布,以防有人遇到这个页面可能会发现它有用.
在Excel VBA IDE中创建一个新模块(可选择UtcConverter在"属性表"中为其指定名称或任何您的首选项)并粘贴到下面的代码中.
HTH
Option Explicit
' Use the PtrSafe attribute for x64 installations
Private Declare PtrSafe Function FileTimeToLocalFileTime Lib "Kernel32" (lpFileTime As FILETIME, ByRef lpLocalFileTime As FILETIME) As Long
Private Declare PtrSafe Function LocalFileTimeToFileTime Lib "Kernel32" (lpLocalFileTime As FILETIME, ByRef lpFileTime As FILETIME) As Long
Private Declare PtrSafe Function SystemTimeToFileTime Lib "Kernel32" (lpSystemTime As SYSTEMTIME, ByRef lpFileTime As FILETIME) As Long
Private Declare PtrSafe Function FileTimeToSystemTime Lib "Kernel32" (lpFileTime As FILETIME, ByRef lpSystemTime As SYSTEMTIME) As Long
Public Type FILETIME
LowDateTime As Long
HighDateTime As Long
End Type
Public Type SYSTEMTIME
Year As Integer
Month As Integer
DayOfWeek As Integer
Day As Integer
Hour As Integer
Minute As Integer
Second As Integer
Milliseconds As Integer
End Type
'===============================================================================
' Convert local time to UTC
'===============================================================================
Public Function UTCTIME(LocalTime As Date) As Date
Dim oLocalFileTime As FILETIME
Dim oUtcFileTime As FILETIME
Dim oSystemTime As SYSTEMTIME
' Convert to a SYSTEMTIME
oSystemTime = DateToSystemTime(LocalTime)
' 1. Convert to a FILETIME
' 2. Convert to UTC time
' 3. Convert to a SYSTEMTIME
Call SystemTimeToFileTime(oSystemTime, oLocalFileTime)
Call LocalFileTimeToFileTime(oLocalFileTime, oUtcFileTime)
Call FileTimeToSystemTime(oUtcFileTime, oSystemTime)
' Convert to a Date
UTCTIME = SystemTimeToDate(oSystemTime)
End Function
'===============================================================================
' Convert UTC to local time
'===============================================================================
Public Function LOCALTIME(UtcTime As Date) As Date
Dim oLocalFileTime As FILETIME
Dim oUtcFileTime As FILETIME
Dim oSystemTime As SYSTEMTIME
' Convert to a SYSTEMTIME.
oSystemTime = DateToSystemTime(UtcTime)
' 1. Convert to a FILETIME
' 2. Convert to local time
' 3. Convert to a SYSTEMTIME
Call SystemTimeToFileTime(oSystemTime, oUtcFileTime)
Call FileTimeToLocalFileTime(oUtcFileTime, oLocalFileTime)
Call FileTimeToSystemTime(oLocalFileTime, oSystemTime)
' Convert to a Date
LOCALTIME = SystemTimeToDate(oSystemTime)
End Function
'===============================================================================
' Convert a Date to a SYSTEMTIME
'===============================================================================
Private Function DateToSystemTime(Value As Date) As SYSTEMTIME
With DateToSystemTime
.Year = Year(Value)
.Month = Month(Value)
.Day = Day(Value)
.Hour = Hour(Value)
.Minute = Minute(Value)
.Second = Second(Value)
End With
End Function
'===============================================================================
' Convert a SYSTEMTIME to a Date
'===============================================================================
Private Function SystemTimeToDate(Value As SYSTEMTIME) As Date
With Value
SystemTimeToDate = _
DateSerial(.Year, .Month, .Day) + _
TimeSerial(.Hour, .Minute, .Second)
End With
End Function
Run Code Online (Sandbox Code Playgroud)
如果您只需要当前时间,则可以使用GetSystemTime执行此操作,这涉及较少的 Win32 调用。它为您提供了一个毫秒精度的时间结构,您可以按照自己的方式格式化:
Private Declare PtrSafe Sub GetSystemTime Lib "Kernel32" (ByRef lpSystemTime As SYSTEMTIME)
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
Run Code Online (Sandbox Code Playgroud)
用法:
Dim nowUtc As SYSTEMTIME
Call GetSystemTime(nowUtc)
' nowUtc is now populated with the current UTC time. Format or convert to Date as needed.
Run Code Online (Sandbox Code Playgroud)