Computer Learning Zone CLZ Access Excel Word Windows

Great men do not seek power; it is thrust upon them.

-Klingon Proverb
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > Code Vault > Generic VBA > Current UTC
Current UTC Time
By Richard Rost   Richard Rost on LinkedIn Email Richard Rost   5 years 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.

Code

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()

Videos

This code is covered in the following videos:

 

Start a NEW Conversation
 
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
 
Subscribe
Subscribe to Current UTC Time
Get notifications when this page is updated
 
 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
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
PCResale.NET
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
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 1/14/2025 8:49:49 PM. PLT: 0s
Keywords: code vault generic vba Daylight Saving Time UTC GMT currentUTC isDST  PermaLink  Current UTC Time