This should be easy as pie: convert local to UTC and back

  • Thread starter Thread starter Chad
  • Start date Start date
C

Chad

I'm looking for the best way to convert a date/time to UTC and back to
Local.

This has to be built in the Framework, but eberything I try gives me
something other than what I expect!

I was tempted to do get the current offset from GMT and use that IN ALL
CASES to convert to and fro, but this offset isn't always the same across
seasons, due to daylights savings, etc.

Here's my sorry excuse for TZ conversion functions. Can someone give me a
better way?

Public Shared Function LocalToUtc(ByVal localTime As Date) As Date
Return DateAdd(DateInterval.Minute, MinuteOffsetFromUtc, localTime)

End Function

Public Shared Function UtlToLocal(ByVal localTime As Date) As Date

Return DateAdd(DateInterval.Minute, -MinuteOffsetFromUtc(), localTime)

End Function

Public Shared Function MinuteOffsetFromUtc() As Long

Static BeenHere As Boolean

Static Offset As Long = Nothing

If Not BeenHere Then

Offset = DateDiff(DateInterval.Minute, My.Computer.Clock.GmtTime,
My.Computer.Clock.LocalTime)

BeenHere = True

End If

Return Offset

End Function
 
Hello,

take a look at the DateTime.SpecifyKind method.

And then there is the DateTime.ToLocal and DateTime.ToUniversal methods.

Kind regards,
Henning

If that doesn't helper
 
[...]
Here's my sorry excuse for TZ conversion functions. Can someone give me
a better way?

If you are having trouble implementing the conversion using VB, you should
post your question either to a more general-purpose newsgroup, or a
newsgroup that is specific to VB instead of C#.

Posting VB code here doesn't really make much sense.
 
Chad said:
I'm looking for the best way to convert a date/time to UTC and back to
Local.

This has to be built in the Framework, but eberything I try gives me
something other than what I expect!

I don't know what the Date type is - I suspect it's something VB.NET
specific - but DateTime (the more commonly used .NET type) has
ToLocalTime and ToUTC. Alternatively if you're using .NET 2.0SP1 or
higher, you can use DateTimeOffset which has more support.
 
[...]

Here's my sorry excuse for TZ conversion functions. Can someone give me
a
better way?

Sigh. Please disregard my previous post. I obviously have not had enough
sleep. I have no idea what newsgroup I'm reading.

Sorry for the interruption. Please continue. :)

By the way, while I am too tired to remember the details off the top of my
head, I can tell you that it is possible to do the conversion you're
asking about. I've got some code somewhere that does it. The only thing
that .NET doesn't support well is dealing with timezones other than the
one the computer is set to.

Pete
 
One of those years already Peter?

:)


Peter Duniho said:
[...]

Here's my sorry excuse for TZ conversion functions. Can someone give me
a
better way?

Sigh. Please disregard my previous post. I obviously have not had enough
sleep. I have no idea what newsgroup I'm reading.

Sorry for the interruption. Please continue. :)

By the way, while I am too tired to remember the details off the top of my
head, I can tell you that it is possible to do the conversion you're
asking about. I've got some code somewhere that does it. The only thing
that .NET doesn't support well is dealing with timezones other than the
one the computer is set to.

Pete
 
On Jan 24, 7:44 am, "Peter Duniho" <[email protected]>
wrote:

By the way, while I am too tired to remember the details off the top of my
head, I can tell you that it is possible to do the conversion you're
asking about. I've got some code somewhere that does it. The only thing
that .NET doesn't support well is dealing with timezones other than the
one the computer is set to.

It's a lot better than it used to be, now that we have DateTimeOffset
and TimeZoneOffset

:)

Jon
 
It's a lot better than it used to be, now that we have DateTimeOffset
and TimeZoneOffset

Sorry, TimeZoneInfo, not TimeZoneOffset. It's one of those days for
me, too.

Jon
 
Back
Top