Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Many times you might be in situation where you have convert UTC time to Local time and Local time to UTC time. Here is the example how to do that.

Step-By-Step Example

- Create a standard exe project
- Add the following code in form code window

Click here to copy the following block
Option Explicit

Private Declare Function GetTimeZoneInformation Lib "kernel32" ( _
    lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

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 As String * 64
  StandardDate As SYSTEMTIME
  StandardBias As Long
  DaylightName As String * 64
  DaylightDate As SYSTEMTIME
  DaylightBias As Long
End Type

Public Function GetUTCBias() As Single
  ' Returns hour offset with West positive, East negative taking
  ' daylight time into account

  Dim dl As Long, dt As Integer
  Dim myTZ As TIME_ZONE_INFORMATION

  dl = GetTimeZoneInformation(myTZ)
  If dl = 2 Then dt = 1 Else dt = 0  'Daylight Time or not
  GetUTCBias = (myTZ.Bias + (myTZ.DaylightBias) * dt) / 60

End Function


Public Function UTCtoLocal(ByVal UTCTime As Date) As Date
  Dim Bias As Single
  Bias = GetUTCBias
  If Bias \ 1 = Bias Then
    UTCtoLocal = DateAdd("h", -1 * Bias, UTCTime)
  Else
    UTCtoLocal = DateAdd("n", -1 * (IIf(Bias < 0, -30, 30)), _
        DateAdd("h", -1 * (Bias \ 1), UTCTime))
  End If
End Function

Public Function LocalToUTC(ByVal LocalTime As Date) As Date
  Dim Bias As Single
  Bias = GetUTCBias
  If Bias \ 1 = Bias Then
    LocalToUTC = DateAdd("h", Bias, LocalTime)
  Else
    LocalToUTC = DateAdd("n", IIf(Bias < 0, -30, 30), _
        DateAdd("h", Bias \ 1, LocalTime))
  End If
End Function

Private Sub Form_Load()
  MsgBox "UTC date/time of your system is :" & LocalToUTC(Now)
  MsgBox "Local date/time converted from current UTC time is :" & UTCtoLocal(LocalToUTC(Now))
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.