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

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

Peter
Thu Jan 24 01:41:54 CST 2008

On Wed, 23 Jan 2008 21:44:25 -0800, Chad <memyselfandirene@comcast.net>
wrote:

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

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

Henning
Thu Jan 24 01:41:14 CST 2008

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
"Chad" <memyselfandirene@comcast.net> wrote in message
news:WP6dncinwf4nuAXanZ2dnUVZ_gqdnZ2d@comcast.com...
> 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
>
>


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

Peter
Thu Jan 24 01:44:35 CST 2008

On Wed, 23 Jan 2008 21:44:25 -0800, Chad <memyselfandirene@comcast.net>
wrote:

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

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

Jon
Thu Jan 24 01:42:06 CST 2008

Chad <memyselfandirene@comcast.net> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

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

Stephany
Thu Jan 24 02:08:10 CST 2008

One of those years already Peter?

:)


"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.t5fbslt78jd0ej@petes-computer.local...
> On Wed, 23 Jan 2008 21:44:25 -0800, Chad <memyselfandirene@comcast.net>
> wrote:
>
>> [...]
>>
>> 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


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

Jon
Thu Jan 24 03:24:27 CST 2008

On Jan 24, 7:44 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

<snip>

> 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

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

Jon
Thu Jan 24 03:25:04 CST 2008

On Jan 24, 9:24 am, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> 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