zipnotic
asked on
Simple Time Conversion question
Hello,
Trying to convert a time given to me in this format:
MyDate = 9/20/2016 4:30:32 PM -07:00
4:30 PM is 'universal time' and -7 is the offset for the time zone in question. I want to get the local time according to the offset, not the time zone on the users computer. So for the above example it should kick back "9/20/2016 9:30 AM" in the variable LocalDate. Having a lot of trouble with something so simple...
Dim LocalDate as datetime
Dim diditwork As Boolean = DateTime.TryParse(MyDate, CultureInfo.CurrentCulture , DateTimeStyles.AssumeUnive rsal, LocalDate)
dim NewDate as DateTime
LocalDate = DateTime.SpecifyKind(Local Date, DateTimeKind.Utc)
NewDate = LocalDate.ToLocalTime
Thanks for ideas
zip
Trying to convert a time given to me in this format:
MyDate = 9/20/2016 4:30:32 PM -07:00
4:30 PM is 'universal time' and -7 is the offset for the time zone in question. I want to get the local time according to the offset, not the time zone on the users computer. So for the above example it should kick back "9/20/2016 9:30 AM" in the variable LocalDate. Having a lot of trouble with something so simple...
Dim LocalDate as datetime
Dim diditwork As Boolean = DateTime.TryParse(MyDate, CultureInfo.CurrentCulture
dim NewDate as DateTime
LocalDate = DateTime.SpecifyKind(Local
NewDate = LocalDate.ToLocalTime
Thanks for ideas
zip
ASKER
Hmm,
Doesn't seem to be working as desired.
Dim utc As DateTime
Dim offset As DateTimeOffset
Dim local As DateTime
DateTime.TryParse("9/20/20 16 4:30:32 PM -07:00", CultureInfo.CurrentCulture , DateTimeStyles.AdjustToUni versal, utc)
offset = New DateTimeOffset(utc, TimeZoneInfo.Utc.GetUtcOff set(utc))
local = offset.DateTime.ToLocalTim e
local still = "9/20/2016 4:30:32 PM"
where it should be "9/20/2016 9:30:32 AM"
watching while debugging it looks like utc is going the wrong way (as in ADDING 7 hours instead of subtracting). The time given to me is UTC and the offset is where I want to go to.
Thanks for you input!
Doesn't seem to be working as desired.
Dim utc As DateTime
Dim offset As DateTimeOffset
Dim local As DateTime
DateTime.TryParse("9/20/20
offset = New DateTimeOffset(utc, TimeZoneInfo.Utc.GetUtcOff
local = offset.DateTime.ToLocalTim
local still = "9/20/2016 4:30:32 PM"
where it should be "9/20/2016 9:30:32 AM"
watching while debugging it looks like utc is going the wrong way (as in ADDING 7 hours instead of subtracting). The time given to me is UTC and the offset is where I want to go to.
Thanks for you input!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sure enough, elegant, simple solution in one line of code. Why do simple problems seem the most perplexing sometimes?
Sheesh, thanks a bunch.
Z
Sheesh, thanks a bunch.
Z
ASKER
Great response and follow up. An elegant solution I wouldn't have thought up in 2 years.
Open in new window
-saige-