convert UTC to local time(system time) and vice vesra in VB.NET

convert UTC to local time(system time) and vice vesra in VB.NET

Post by ZGVlcGF » Thu, 07 Feb 2008 19:00:01


i All,

I want to convert the UTC time to local time zone(system time zone) and vice
versa in ASP.NET using vb.Net

currenlty i know this function which i took from internet site but this
function only converts from UTC to local and not vice versa. I need a
function which converts from utc to local and loac to utc time format for all
dates which is recevied by the ASP.NET
interface in UTC.
this interface will recieve the time in UTC and will convert using a vb.net
function intrenally into LOCAL and finally while again sending back it will
convert to UTC again and will send back to interfce.
So kindlt let me know a function which return UTC to Local(regisrty time of
machine based on time zone) and another function for local to UTC.

My current function for UTC -> LOCAL IS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Imports Microsoft.Win32
Imports System.Runtime.InteropServices

Public Class timezone_converter
Private Structure TZI
Dim bias As Integer
Dim standardBias As Integer
Dim daylightBias As Integer
Dim standardDate As SYSTEMTIME
Dim daylightDate As SYSTEMTIME
End Structure

Public Structure TIME_ZONE_INFORMATION
Dim Bias As Integer
<VBFixedArray(32)> Dim StandardName() As Short
Dim StandardDate As SYSTEMTIME
Dim StandardBias As Integer
<VBFixedArray(32)> Dim DaylightName() As Short
Dim DaylightDate As SYSTEMTIME
Dim DaylightBias As Integer

Public Sub Initialize()
ReDim StandardName(32)
ReDim DaylightName(32)
End Sub
End Structure
Public Structure SYSTEMTIME
Dim wYear As Integer
Dim wMonth As Integer
Dim wDayOfWeek As Integer
Dim wDay As Integer
Dim wHour As Integer
Dim wMinute As Integer
Dim wSecond As Integer
Dim wMilliseconds As Integer
End Structure

Public Function UTCtoLocal(ByVal serverdate As DateTime, ByVal tzone As
String) As DateTime
Dim timezoneinfo As TIME_ZONE_INFORMATION
timezoneinfo.Initialize()
Dim servertime As SYSTEMTIME
Dim serverfiletime As New FILETIME
Dim Localfiletime As New FILETIME
Dim Localtime As SYSTEMTIME
Dim custTZI As TZI
Dim strTZI() As Byte
Dim temp1 As Long
Dim temp2 As Integer


Dim strPath As String = "Software\Microsoft\Windows
NT\CurrentVersion\Time Zones\" & tzone
Dim oReg As RegistryKey = Registry.LocalMachine.OpenSubKey(strPath)
strTZI = oReg.GetValue("TZI")
custTZI = BulidStr(strTZI)

timezoneinfo.Bias = custTZI.bias
timezoneinfo.StandardBias = custTZI.standardBias
timezoneinfo.StandardDate = custTZI.standardDate
timezoneinfo.DaylightBias = custTZI.daylightBias
timezoneinfo.DaylightDate = custTZI.daylightDate

temp1 = serverdate.Ticks

serverfiletime.dwHighDateTime = (temp1 >> 32)
temp2 = temp1 And &HFFFFFFFFL
serverfiletime.dwLowDateTime = Convert.ToInt32(temp2.ToString)

FileTimeToSystemTime(serverfiletime, servertime)
' TzSpecificLocalTimeToSystemTime(timezoneinfo, servertime, Localtime)
SystemTimeToTzSpecificLocalTime(timezoneinfo, servertime, Localtime)
SystemTimeToFileTime(Localtime,
 
 
 

convert UTC to local time(system time) and vice vesra in VB.NET

Post by Kevin Spen » Thu, 07 Feb 2008 23:34:58

ood heavens, dude, all you need is the DateTime class. Everything you need
to convert it is there:

http://msdn2.microsoft.com/en-us/library/system.datetime.aspx

Specifically, look at the following properties and methods:

DateTime.Kind (indicates whether it is a UTC or Local DateTime instance)
DateTime.UtcNow (provides the current DateTime's UTC DateTime value)
DateTime.ToUniversalTime (converts a local DateTime instance to UTC)
DateTime.ToLocalTime (converts a UTC DateTime to local)

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"deepak" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...