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

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
 
H

Henning Krause [MVP - Exchange]

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
 
P

Peter Duniho

[...]
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.
 
J

Jon Skeet [C# MVP]

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.
 
P

Peter Duniho

[...]

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
 
S

Stephany Young

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
 
J

Jon Skeet [C# MVP]

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
 
J

Jon Skeet [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top