-
Jul 23rd, 2024, 11:58 AM
#1
Thread Starter
New Member
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
-
Jul 23rd, 2024, 01:26 PM
#2
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
-
Jul 23rd, 2024, 01:31 PM
#3
Thread Starter
New Member
Re: date time convert with hours offset
Originally Posted by dday9
-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.
-
Jul 23rd, 2024, 02:33 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 23rd, 2024, 02:34 PM
#5
Re: date time convert with hours offset
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 23rd, 2024, 03:15 PM
#6
Re: date time convert with hours offset
Originally Posted by jwyman
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
-
Jul 24th, 2024, 04:28 PM
#7
Thread Starter
New Member
Re: date time convert with hours offset
Originally Posted by dday9
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?
-
Jul 24th, 2024, 04:45 PM
#8
Re: date time convert with hours offset
A numeric offset value converted to a string
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 24th, 2024, 07:20 PM
#9
Re: date time convert with hours offset
Just like what you posted in your original post: 2024-07-01T11:00:00-08:00
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|