+ Reply to Thread
Results 1 to 2 of 2

Getting UTC offset for dates different then 'right now'

Hybrid View

JasperD Getting UTC offset for dates... 09-13-2018, 04:33 AM
bakerman2 Re: Getting UTC offset for... 09-13-2018, 09:38 PM
  1. #1
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Getting UTC offset for dates different then 'right now'

    Hi all,

    I currently use the code below to get the UTC offset between my system time/date and the UTC time/date

    Public Declare PtrSafe Sub GetSystemTime Lib "Kernel32" (ByRef lpSystemTime As U)
    Public Type U
    Ye As Integer: Mo As Integer:   WD As Integer:    Da As Integer:    Hr As Integer:    Mn As Integer:    Se As Integer:    Ms As Integer
    End Type
    Sub GetUTCOffset()
    Dim nU As U, UD As Date
    Call GetSystemTime(nU)
    UD = nU.Ye & "-" & nU.Mo & "-" & nU.Da & " " & nU.Hr & ":" & nU.Mn & ":" & nU.Se
    Debug.Print Format(Now - UD, "HH:NN")
    End Sub
    This works great - for "right now" and returns 2hrs in my timezone.
    But due to daylight saving time being different across the world, if I take let's say the 2hr offset and apply it for something I plan in November, it'll be wrong (as it'll be 1hr by then).
    Is there an _easy_ way to check the UTC offset for different dates?
    Thanks!

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,308

    Re: Getting UTC offset for dates different then 'right now'

    Following code isn't mine and I don't rememeber where I got so I can't give proper credit but it seems to do what you're after.
    ' System time structure
    Public 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
    
    ' Time zone information. Note that this one is defined wrong in API viewer.
    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 Declare Function SystemTimeToTzSpecificLocalTime Lib "Kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION, lpUniversalTime As SYSTEMTIME, lpLocalTime As SYSTEMTIME) As Long
    Private Declare Function GetTimeZoneInformation Lib "Kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
    
    ' Convert UTC time to local time for current time zone
    Public Function UTCtoLocal(ByVal tDate As Date) As Date
        Dim tzi As TIME_ZONE_INFORMATION
        Dim stUTC As SYSTEMTIME
        Dim stLocal As SYSTEMTIME
        Dim lres As Long
        
        lres = GetTimeZoneInformation(tzi)
        stUTC.wYear = Year(tDate)
        stUTC.wMonth = Month(tDate)
        stUTC.wDay = Day(tDate)
        stUTC.wHour = Hour(tDate)
        stUTC.wMinute = Minute(tDate)
        stUTC.wSecond = Second(tDate)
        stUTC.wMilliseconds = 0
        lres = SystemTimeToTzSpecificLocalTime(tzi, stUTC, stLocal)
        UTCtoLocal = DateSerial(stLocal.wYear, stLocal.wMonth, stLocal.wDay) + TimeSerial(stLocal.wHour, stLocal.wMinute, stLocal.wSecond)
    End Function
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2014, 10:26 AM
  2. Show/sort dates that fall in between a date (offset?)
    By IKZOUHETNIETWETEN in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-28-2014, 01:51 PM
  3. Adding Between Dates, Large, Max, Offset Formula Help Required
    By jenita.kurlawala in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-03-2013, 06:42 AM
  4. [SOLVED] Offset, Index, Match formula with dates inconsistently working
    By David Brown in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-31-2013, 04:12 PM
  5. function similar to offset to offset the offset
    By Jerseynjphillypa in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-03-2012, 03:07 PM
  6. Reformatting - insert formula with offset, then offset to next cell
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-06-2011, 05:41 AM
  7. Replies: 7
    Last Post: 11-04-2008, 06:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1