Computer Learning Zone CLZ Access Excel Word Windows

All the world is a laboratory to the inquiring mind.

-Martin Fisher
 
Home   Courses   Seminars   Templates   Help   TechHelp   Forums   Contact   Join   Order   Logon  
 
Home > Code Vault > Generic VBA > Current UTC
 
Current UTC Time
By Richard Rost   Richard Rost on Twitter Richard Rost on LinkedIn Email Richard Rost   14 months ago

The CurrentUTC function will return the current time in UTC (Universal Time Coordinated) based on the system clock. It uses the IsDST function to determine whether or not Daylight Saving Time is in effect. Copy all of the following code into a global Module. You need to set the constant UTC_OFFSET for your area, for example Eastern Standard Time is normally UTC-5.

Const UTC_OFFSET = -5    ' Eastern Standard Time (UTC-5)
    
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 Declare PtrSafe Function GetTimeZoneInformation Lib "kernel32" ( _
    lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
        
Public Function CurrentUTC() As Date
    Dim Offset As Integer
    Offset = UTC_OFFSET * -1
    If IsDST() Then Offset = Offset - 1
    CurrentUTC = DateAdd("h", Offset, Now())
End Function
    
Public Function IsDST() As Boolean
    ' 1 = Standard, 2 = Daylight, 0 = Unknown
    Dim TZInfo As TIME_ZONE_INFORMATION
    Dim L As Long
    L = GetTimeZoneInformation(TZInfo)
    If L = 2 Then
        IsDST = True
    Else
        IsDST = False
    End If
End Function

Usage:

MyLocalTime = Now()
MyUTCTime = CurrentUTC()

 

Start a NEW Conversation
 
Only students may post right now. Click here for more information on how you can set up an account. If you are a student, please LOG ON first.
 
Subscribe
 
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn
Keywords: code vault generic vba Daylight Saving Time UTC GMT currentUTC isDST  PermaLink