Results 1 to 4 of 4

Thread: [RESOLVED] Server time gets automatically converted.

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    38

    Resolved [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
    Last edited by thatsasif; Mar 17th, 2010 at 11:11 AM.
    <===>Asif Maknojia<===>

  2. #2

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    38

    Re: Server time gets automatically converted.

    Can anybody tell me how to convert +1100 to TimeSpan
    <===>Asif Maknojia<===>

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    38

    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.
    <===>Asif Maknojia<===>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width