[RESOLVED] Server time gets automatically converted.
Hi,
I am having problem with the date time. I am getting the time from the server in xml, when i try to assign the date which i get from the server to date variable it automatically converts to local time. Is there any way to prevent this conversion.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
convertDateTime("Fri Feb 26 23:01:50 +1100 2010")
'The above date format is received from the server in xml data
'+1100 is Australian East Daylight
End Sub
Private Function convertDateTime(ByVal dt As String) As DateTime
Dim strDateTime() As String
Dim strDay As String
Dim strMonth As String
Dim strDate As String
Dim strTime As String
Dim strYear As String
Dim strZone As String
Dim _dateTime As New DateTime
Dim newTime As New DateTime
strDateTime = Split(dt)
strDay = strDateTime(0)
strMonth = strDateTime(1)
strDate = strDateTime(2)
strTime = strDateTime(3)
strYear = strDateTime(5)
strZone = strDateTime(4)
'Below Format "Sun, 09 Mar 2008 16:05:07 Zone"
_dateTime = strDay & ", " & strDate & " " & strMonth & " " & strYear & " " & strTime & " " & strZone
Console.WriteLine("Server " + _dateTime)
newTime = Convert.ToDateTime(_dateTime)
Console.WriteLine("Local " + newTime)
Return newTime
End Function
End Class
The output of the above code is...
Code:
Server 2/26/2010 5:31:50 PM
Local 2/26/2010 5:31:50 PM
Re: Server time gets automatically converted.
Can anybody tell me how to convert +1100 to TimeSpan
Re: Server time gets automatically converted.
All the functionality you want is built into the Framework:
Code:
Dim str = "Fri Feb 26 23:01:50 +1100 2010"
Dim local = Date.ParseExact(str, "ddd MMM dd HH:mm:ss zzz yyyy", Nothing)
Dim utc = local.ToUniversalTime()
MessageBox.Show(local.ToString(), "Local")
MessageBox.Show(utc.ToString(), "UTC")
Re: Server time gets automatically converted.
Thanks jmcilhinney
Dim local = Date.ParseExact(str, "ddd MMM dd HH:mm:ss zzz yyyy", Nothing) very good solution parsing the date for the above format.
Actually i wanted the server time to be the same and not UTC so i did it this way
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
convertDateTime("Fri Feb 26 23:01:50 +1100 2010")
'The above date format is received from the server in xml data
'+1100 is Australian East Daylight
End Sub
Private Function convertDateTime(ByVal dt As String) As DateTime
Dim strDateTime() As String
Dim strDay, strMonth, strDate, strTime, strYear, strZone As String
Dim _dateTime As New DateTime
'_dateTime = DateTime.ParseExact(dt, "ddd MMM dd HH:mm:ss zzz yyyy", Nothing)
strDateTime = Split(dt)
strDay = strDateTime(0)
strMonth = strDateTime(1)
strDate = strDateTime(2)
strTime = strDateTime(3)
strYear = strDateTime(5)
strZone = strDateTime(4)
'Below Format "Sun, 09 Mar 2008 16:05:07 Zone"
If convertTimezone Then
'Timezone included to change to local time.
_dateTime = strDay & ", " & strDate & " " & strMonth & " " & strYear & " " & strTime & " " & strZone
Console.WriteLine("Local" + _dateTime)
Else
'Timezone not included to keep server time.
_dateTime = strDay & ", " & strDate & " " & strMonth & " " & strYear & " " & strTime
Console.WriteLine("Server " + _dateTime)
End If
Return _dateTime
End Function
This was good option because i was not showing the timezone.