2009-10-21 31 views

Trả lời

9

http://excel.tips.net/Pages/T002185_Automatically_Converting_to_GMT.html

Có một vĩ mô trên trang đó với một phương pháp LocalTimeToUTC. Có vẻ như nó sẽ làm trò lừa. Ngoài ra một số ví dụ công thức nếu bạn muốn đi tuyến đường đó.

Chỉnh sửa - Liên kết khác. http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx Trang này có một số phương pháp cho ngày/giờ. Chọn thuốc độc của bạn. Hoặc là nên làm các trick, nhưng tôi cảm thấy như thứ hai là đẹp hơn. ;)

+0

Rất tiếc, cả hai đều sử dụng các cuộc gọi Windows API, nhưng nếu đó là cách duy nhất thì hãy làm như vậy. Cảm ơn. – Jon

7

Cấp câu hỏi này cũ, nhưng tôi đã dành một chút thời gian để tập hợp một số mã sạch dựa trên điều này và tôi muốn đăng nó ở đây trong trường hợp bất kỳ ai truy cập trang này có thể thấy hữu ích.

Tạo mô-đun mới trong IDE VBA Excel (tùy chọn đặt tên là UtcConverter hoặc bất kỳ tùy chọn nào của bạn có thể nằm trong Trang thuộc tính) và dán vào mã bên dưới.

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 
0

Nếu bạn cũng cần phải giải thích cho tiết kiệm ánh sáng ban ngày thời gian, bạn có thể tìm thấy đoạn mã sau hữu ích:

Option Explicit 

''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' Windows API Structures 
''''''''''''''''''''''''''''''''''''''''''''''''''''' 
Private Type SYSTEM_TIME 
    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 SYSTEM_TIME 
    StandardBias As Long 
    DaylightName(0 To 31) As Integer 
    DaylightDate As SYSTEM_TIME 
    DaylightBias As Long 
End Type  

''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' Windows API Imports 
''''''''''''''''''''''''''''''''''''''''''''''''''''' 
Private Declare Function GetTimeZoneInformation Lib "kernel32" _ 
    (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long 

Private Declare Function TzSpecificLocalTimeToSystemTime Lib "kernel32" _ 
    (lpTimeZoneInformation As TIME_ZONE_INFORMATION, lpLocalTime As SYSTEM_TIME, lpUniversalTime As SYSTEM_TIME) As Integer 

Function ToUniversalTime(localTime As Date) As Date 
    Dim timeZoneInfo As TIME_ZONE_INFORMATION 

    GetTimeZoneInformation timeZoneInfo 

    Dim localSystemTime As SYSTEM_TIME 
    With localSystemTime 
     .wYear = Year(localTime) 
     .wMonth = Month(localTime) 
     .wDay = Day(localTime) 
    End With 

    Dim utcSystemTime As SYSTEM_TIME 

    If TzSpecificLocalTimeToSystemTime(timeZoneInfo, localSystemTime, utcSystemTime) <> 0 Then 
     ToUniversalTime = SystemTimeToVBTime(utcSystemTime) 
    Else 
     err.Raise 1, "WINAPI", "Windows API call failed" 
    End If 

End Function 

Private Function SystemTimeToVBTime(systemTime As SYSTEM_TIME) As Date 
    With systemTime 
     SystemTimeToVBTime = DateSerial(.wYear, .wMonth, .wDay) + _ 
       TimeSerial(.wHour, .wMinute, .wSecond) 
    End With 
End Function 
3

Nếu tất cả bạn cần là thời gian hiện tại, bạn có thể làm điều này với GetSystemTime, liên quan đến các cuộc gọi Win32 ít hơn. Nó mang đến cho bạn một cấu trúc thời gian, với độ chính xác phần nghìn giây, mà bạn có thể định dạng như thế nào bạn muốn:

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 

Cách sử dụng:

Dim nowUtc As SYSTEMTIME 
Call GetSystemTime(nowUtc) 
' nowUtc is now populated with the current UTC time. Format or convert to Date as needed. 
0

dự án truy cập của tôi làm việc với hầu hết bảng truy cập liên quan đến MS SQL Server những cái bàn. Nó là một dự án DAO và tôi đã gặp rắc rối ngay cả khi nhận được một sproc SQL với GETUTCDATE() để quay trở lại. Nhưng sau đây là giải pháp của tôi.

-- Create SQL table with calculated field for UTCDate 
CREATE TABLE [dbo].[tblUTCDate](
    [ID] [int] NULL, 
    [UTCDate] AS (getutcdate()) 
) ON [PRIMARY] 
GO 

Tạo bảng Access, dbo_tblUTCDate, được liên kết qua ODBC với bảng SQL tblUTCDate.

Tạo truy vấn Access để chọn từ bảng Access. Tôi gọi nó là qryUTCDate.

SELECT dbo_tblUTCDate.UTCDate FROM dbo_tblUTCDate 

Trong VBA:

Dim db as DAO.database, rs AS Recordset 
Set rs = db.OpenRecordset("qryUTCDate") 
Debug.Print CStr(rs!UTCDATE) 
rs.Close 
Set rs = Nothing 
db.Close 
Set db = Nothing 
2

Đơn giản, bạn có thể sử dụng COM Object để đạt được tính theo giờ UTC Thông tin Time.

Dim dt As Object, utc As Date 
Set dt = CreateObject("WbemScripting.SWbemDateTime") 
dt.SetVarDate Now 
utc = dt.GetVarDate(False) 
+0

Tuyệt vời! Yếu tố này có phải là DST không? @gogeek – MeenakshiSundharam

Các vấn đề liên quan