Excel VBA 매크로에서 UTC의 현재 날짜 시간을 가져오는 방법
Excel VBA 매크로에서 현재 날짜를 UTC 형식으로 가져올 수 있는 방법이 있습니까?
전화할 수 있습니다Now()
현지 시간대에서 현재 시간을 얻는 것; 그런 다음 이것을 UTC로 변환하는 일반적인 방법이 있습니까?
간단히 COM 개체를 사용하여 UTC 시간 정보를 얻을 수 있습니다.
Dim dt As Object, utc As Date
Set dt = CreateObject("WbemScripting.SWbemDateTime")
dt.SetVarDate Now
utc = dt.GetVarDate(False)
http://excel.tips.net/Pages/T002185_Automatically_Converting_to_GMT.html
해당 페이지에 LocalTimeTo가 있는 매크로가 있습니다.UTC 방식효과가 있을 것 같네요.또한 그 길을 가고자 한다면 몇 가지 공식 예를 들 수 있습니다.
편집 - 다른 링크입니다.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
현재 시간만 필요한 경우 Win32 호출 수가 적은 GetSystemTime을 사용하여 이 작업을 수행할 수 있습니다.밀리초 단위의 정밀도로 원하는 형식을 지정할 수 있는 시간 구조를 제공합니다.
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
용도:
Dim nowUtc As SYSTEMTIME
Call GetSystemTime(nowUtc)
' nowUtc is now populated with the current UTC time. Format or convert to Date as needed.
안녕하세요. 질문은 "Windows용 Excel VBA"가 아니라 Excel VBA에 관한 것입니다.간단히 말해서, 아무리 찬성해도 Mac이나 Linux에서 작동하는 답변은 없습니다(예, Office, 적어도 Office 2000은 Linux에서도 실행됩니다).
그래서 제 대답은 이렇습니다.0표를 얻는 것은 예측 가능하지만, 진실은 미인대회가 아닙니다.
Windows(윈도우)의 경우 다른 답변(자동으로 Windows를 가정하고 프로그래머가 상황을 가정하여 문제를 만드는 방법을 보여주는 답변)을 참조하십시오.
Mac의 경우 https://macscripter.net/viewtopic.php?id=41117 을 참조하십시오.
Linux의 경우 Unix에서 GMT 시간을 얻는 방법을 참조하십시오.
VBA랑 재밌게 놀아요.너무 건조해서 미안하지만, 주변에 너무 많은 근사치가 돌고 있어, S.O.는 이제 무서워집니다.
일광 절약 시간도 고려해야 하는 경우 다음 코드가 유용할 수 있습니다.
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
My Access 프로젝트는 대부분 MS SQL Server 테이블에 연결된 Access 테이블에서 작동합니다.DAO 프로젝트이며 GETUTCDATE()가 포함된 SQL 프로시저를 다시 가져오는데도 어려움을 겪었습니다.하지만 다음은 제 해결책이었습니다.
-- Create SQL table with calculated field for UTCDate
CREATE TABLE [dbo].[tblUTCDate](
[ID] [int] NULL,
[UTCDate] AS (getutcdate())
) ON [PRIMARY]
GO
액세스 테이블, dbo_tbl 생성UTCDate, ODBC를 통해 SQL 테이블 tblUTCDate에 연결됩니다.
액세스 테이블에서 선택할 액세스 쿼리를 만듭니다.나는 그것을 qury라고 불렀습니다.UTC 날짜.
SELECT dbo_tblUTCDate.UTCDate FROM dbo_tblUTCDate
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
언급URL : https://stackoverflow.com/questions/1600875/how-to-get-the-current-datetime-in-utc-from-an-excel-vba-macro
'programing' 카테고리의 다른 글
Oracle을 사용하여 Oracle에 연결합니다.관리되는 데이터 액세스 (0) | 2023.07.17 |
---|---|
스프링 부츠 플라이웨이예외:데이터베이스에 연결할 수 없습니다.URL, 사용자 및 암호 구성 (0) | 2023.07.17 |
numpy 배열을 통해 반복 (0) | 2023.07.17 |
두 하위 플롯이 생성된 후 x축을 공유하는 방법 (0) | 2023.07.17 |
드롭다운 목록 워드프레스에 상위 & 1단계 아동 범주 표시 (0) | 2023.07.12 |