Conversion Rome time to New York Time

P

pamela fluente

Say that I have the variable "RomeDate" containing a date with my
local time, in Rome, Italy.

I wish a function to convert RomeDate to another datetime variable
containing the same date
but with NY time:

Function RomeToNewYorkTime(RomeDate as Date) as Date

dim NYDate as date = ... ?? ...
return NYDate

end function


How can I do that in such a way it works for *any* day or month of the
year ?
[clearly I know that, at this time, there's 4 hours difference, but I
want a function which would work *in general*
whatever, day or month it is]

I know it is a trivial question but I am problem finding the VB
classes to handle the respective legal ("summer") times.

Thank you very much for any help,

P
 
T

Terry

Well I have never actually used it.... but it look to me like you would use:

Public Shared Function ConvertTime ( _
dateTime As DateTime, _
sourceTimeZone As TimeZoneInfo, _
destinationTimeZone As TimeZoneInfo _
) As DateTime

Before using it, you will need to instanciate the source(Rome) and
destination(NY) TimeZoneInfo by using one on the Shared/Static members to do
this.
Either find the Id's for te 2 zones or create custom zones for the 2 cities,
you just have to know the 'UTC' offset for each.
Havent got more time to actually do this for you, but it sounds doable.
 
T

Terry

Here, this may help:

Dim RomeZone As TimeZoneInfo= TimeZoneInfo.CreateCustomTimeZone("Rome",new
timespan(1,0,0), "Rome", "Rome Std Time")
Dim LocalZone As TimeZoneInfo = TimeZoneInfo.Local

then it is
LocalTime = TimeZoneInfo.ConvertTime(TheDateTime, RomeZone, LocalZone)

Looking back at your original post, a better approach may be for you to
first convert it to UDC time from your local time and then anyone who wants
to read it can convert it from UDC time to their local time, thereby
elimenating the need to know the timezone of the person who stored it.
 
P

pamela fluente

Here, this may help:

Dim RomeZone As TimeZoneInfo= TimeZoneInfo.CreateCustomTimeZone("Rome",new
timespan(1,0,0), "Rome", "Rome Std Time")
 Dim LocalZone As TimeZoneInfo = TimeZoneInfo.Local

then it is
LocalTime = TimeZoneInfo.ConvertTime(TheDateTime, RomeZone, LocalZone)

Looking back at your original post, a better approach may be for you to
first convert it to UDC time from your local time and then anyone who wants
to read it can convert it from UDC time to their local time, thereby
elimenating the need to know the timezone of the person who stored it.

--
Terry










- Mostra testo tra virgolette -

Thanks a lot Terry. That's very helpful.

Do you also know, by chance, if it works with framework 2 too?

I was doing some attempts of implementation but my VS (2005) seemed
not to be able to recognize the class.

Perhaps it's only for 3.5 (?)

-P
 
T

Terry

Sorry, but it looks like the TimeZoneInfo class is new to 3.0/3.5, but there
is still the TimeZone class. It does not let you convert between zones
directly, but it does let you convert to/from UTC.
So, when storing the date...
Dim MyZone as New TimeZone
Dim TheStoredDate as DateTime = MyZone.ToUniversalTime(Now())
....
....
and when retreiving TheStoredDate
Dim MyZone as New TimeZone
Dim LocalDate as DateTime = MyZone.ToLocalTime(TheStoredDate)

HTH,
 
P

pamela fluente

Sorry, but it looks like the TimeZoneInfo class is new to 3.0/3.5, but there
is still the TimeZone class.  It does not let you convert between zones
directly, but it does let you convert to/from UTC.
So, when storing the date...
Dim MyZone as New TimeZone
Dim TheStoredDate as DateTime = MyZone.ToUniversalTime(Now())
...
...
and when retreiving TheStoredDate
Dim MyZone as New TimeZone
Dim LocalDate as DateTime = MyZone.ToLocalTime(TheStoredDate)

HTH,
--
Terry










- Mostra testo tra virgolette -

Thanks a lot Terry. Very helpful, indeed.

Ciao,

-P
 

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