Results 1 to 2 of 2

Thread: International Date Time conversion

  1. #1
    gchq's Avatar
    gchq is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2007
    Posts
    168
    Reputation
    116

    International Date Time conversion

    Hi there

    We have a desktop WinApp that saves data to our server via a WebService

    Our servers are set to UTC

    Question is - what is the best method of saving dates locally and then retrieving them?

    I have tried several methods like

    Code:
    DateTime.Parse(InputDate).ToUniversalTime
    to save the date

    and

    Code:
    DateTime.Parse(InputDate).ToLocalTime
    to display the date...

    But to no avail!

    Any ideas on the best method?

    Thanks


    ==========================================

    Just to add a bit of history

    At first dates were saved and displayed 'as is' with no conversion (a string, is a string, is a string... what needs to be converted?)

    Then I discovered that data saved with a GB regional setting was very helpfully (not) showing as the day before at 16:00 (PST), sooooo..

    Code:
     Public Function ReturnLocalDateFormat(ByVal InputDate As Date) As String
            InputDate = DateTime.Parse(InputDate).ToUniversalTime
                   Dim vCulture As String = System.Globalization.CultureInfo.CurrentCulture.ToString
            Dim vReturnDate As String = ""
            Select Case vCulture
                Case "en-US"
                    vReturnDate = Format(InputDate, "MM/dd/yyyy")
                Case "en-GB"
                    vReturnDate = Format(InputDate, "dd/MM/yyyy")
                Case Else
                    vReturnDate = Format(InputDate, "dd/MM/yyyy")
            End Select
            Return vReturnDate    
        End Function
    ... seemed to sort out the problem, and we had peace within developer land!

    Then it was discovered that entering an item on the PST PC before 08:00 would display the date as the day before...

    Since then it's been all downhill with every new method attempted...
    Last edited by gchq; 04-09-2010 at 1:28 PM.

  2. #2
    gchq's Avatar
    gchq is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2007
    Posts
    168
    Reputation
    116

    *******!!! Daylight Saving Hours *****!!!

    After it dawned on me that there was no problem with dates before the clocks went forward, the solution was this:-

    Code:
    Dim LocalZone As TimeZone = TimeZone.CurrentTimeZone
            Dim CurrentOffset As TimeSpan = LocalZone.GetUtcOffset(InputDate)
            Dim DayLightSaving As Boolean = LocalZone.IsDaylightSavingTime(InputDate)
            Dim CalculatedOffset As New DateTime(InputDate.Ticks, DateTimeKind.Local)
            If CurrentOffset.CompareTo(TimeSpan.Zero) < 0 Then
                CalculatedOffset -= LocalZone.GetUtcOffset(InputDate)
                If DayLightSaving = True Then
                    CalculatedOffset = CalculatedOffset.AddHours(1)
                End If
            Else
                CalculatedOffset += LocalZone.GetUtcOffset(InputDate)
                If DayLightSaving = True Then
                    CalculatedOffset = CalculatedOffset.AddHours(-1)
                End If
            End If
    
            InputDate = CalculatedOffset

Bookmarks

Posting Permissions

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