Results 1 to 9 of 9

Thread: date time convert with hours offset

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    8

    date time convert with hours offset

    Hello all,

    I am trying to convert "2024-07-01T11:00:00-08:00" to "858 PM EDT Sun Jun 30 2024".

    There is an UTC hour offset (-8:00) which converts to the local time (whatever that is).

    I am trying to determine the current time zone string (EDT,CDT,PDT, etc.) from the first string with hour offset.

    Need to take into account daylight savings time or not.

    Thanks All!

    Jim

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,966

    Re: date time convert with hours offset

    -08:00 represents the pacific TimeZone. Your DateTimeOffset literal does not equal 858 PM EDT Sun Jun 30 2024 by the way, it equals 3PM July 1 for EST.

    What you can do is parse the DateTimeOffset, get the desired TimeZone by the string, then convert the parsed DateTimeOffset using the matched TimeZone:
    Code:
    Private Function ParseDateTimeOffsetToSpecificTimeZone(literal As String, timezone As String) As DateTime
        Dim parsedDateTimeOffset = DateTimeOffset.Parse(literal, Nothing, DateTimeStyles.AssumeUniversal)
        Dim desiredTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timezone)
        Dim conversion = TimeZoneInfo.ConvertTime(parsedDateTimeOffset, desiredTimeZone)
        Return conversion.DateTime
    End Function
    Fiddle: https://dotnetfiddle.net/cMZ0DH
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    8

    Re: date time convert with hours offset

    Quote Originally Posted by dday9 View Post
    -08:00 represents the pacific TimeZone. Your DateTimeOffset literal does not equal 858 PM EDT Sun Jun 30 2024 by the way, it equals 3PM July 1 for EST.

    What you can do is parse the DateTimeOffset, get the desired TimeZone by the string, then convert the parsed DateTimeOffset using the matched TimeZone:
    Code:
    Private Function ParseDateTimeOffsetToSpecificTimeZone(literal As String, timezone As String) As DateTime
        Dim parsedDateTimeOffset = DateTimeOffset.Parse(literal, Nothing, DateTimeStyles.AssumeUniversal)
        Dim desiredTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timezone)
        Dim conversion = TimeZoneInfo.ConvertTime(parsedDateTimeOffset, desiredTimeZone)
        Return conversion.DateTime
    End Function
    Fiddle: https://dotnetfiddle.net/cMZ0DH
    Thanks. I just used that example for formatting purposes only. I knew that the times didn't match.

    my wish is to get the timezone string (i.e. PDT) from the hour offset. And it needs to automatically account for savings time.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,669

    Re: date time convert with hours offset

    https://learn.microsoft.com/en-us/do...e?view=net-8.0

    If daylight saving is in force, it’ll be automatically applied

  5. #5

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,966

    Re: date time convert with hours offset

    Quote Originally Posted by jwyman View Post
    My wish is to get the timezone string (i.e. PDT) from the hour offset.
    You will need to get the Offset from the parsed DateTimeOffset value then loop over the TimeZones checking which offset matches:
    Code:
    Private Function GetTimeZoneStringFromOffset(literal As String) As String
        Dim parsedDateTimeOffset = DateTimeOffset.Parse(literal, Nothing, DateTimeStyles.AssumeUniversal)
        Dim offset = parsedDateTimeOffset.Offset
        Dim timeZones = TimeZoneInfo.GetSystemTimeZones()
    
        For Each timeZone In timeZones
            If (timeZone.BaseUtcOffset = offset) Then
                Return timeZone.Id
            End If
        Next
    
        Return Nothing
    End Function
    Fiddle: https://dotnetfiddle.net/hauxnP
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2022
    Posts
    8

    Re: date time convert with hours offset

    Quote Originally Posted by dday9 View Post
    You will need to get the Offset from the parsed DateTimeOffset value then loop over the TimeZones checking which offset matches:
    Code:
    Private Function GetTimeZoneStringFromOffset(literal As String) As String
        Dim parsedDateTimeOffset = DateTimeOffset.Parse(literal, Nothing, DateTimeStyles.AssumeUniversal)
        Dim offset = parsedDateTimeOffset.Offset
        Dim timeZones = TimeZoneInfo.GetSystemTimeZones()
    
        For Each timeZone In timeZones
            If (timeZone.BaseUtcOffset = offset) Then
                Return timeZone.Id
            End If
        Next
    
        Return Nothing
    End Function
    Fiddle: https://dotnetfiddle.net/hauxnP
    what is the literal format for the input of this function? a date time string or a numeric offset value?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,669

    Re: date time convert with hours offset

    A numeric offset value converted to a string

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,966

    Re: date time convert with hours offset

    Just like what you posted in your original post: 2024-07-01T11:00:00-08:00
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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