An investment in knowledge
pays the best interest.
-Benjamin Franklin
 
Home   News   Courses   Seminars   Templates   Help   TechHelp   Forums   Order   Contact   Logon  
 
Home > Code Vault > Generic VBA > Current UTC
 
Current UTC Time
By Richard Rost   Richard Rost on Facebook Richard Rost on Twitter Richard Rost on LinkedIn Email Richard Rost   3 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()

 

Post a New Comment or Question
 
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
Free Upgrades
Insider Circle
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Articles
Search The Site
Waiting List
Production Schedule
Code Vault
Collapse Menus
Help
 
Live Chat
Customer Support
WalkThru Tutorials
Troubleshooting
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
MYOLP Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Live Chat
General Info
Support Policy
Contact Form
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Facebook    Twitter

YouTube Channel    LinkedIn

Blog RSS Feed   
Keywords: code vault generic vba Daylight Saving Time UTC GMT currentUTC isDST  PermaLink