Convert US Eastern Time to local .Net time?

  • Thread starter Thread starter Mark B
  • Start date Start date
M

Mark B

We have a .Net VSTO Add-in for Outlook 2007.

We receive a version DateTime field from our ASP.NET webservice from a
server in Florida, e.g. dtServerDateTime in Eastern Time Zone DateTime.

How can I convert this DateTime variable to the user's local computer time
so I can display it on a form on their desktop? They may be in Brazil, the
UK or another location.

I am assuming our shared server hosted by serverintellect.com doesn't adjust
for daylight savings too... If so I would need to take account of that and
the local user's daylight saving I am guessing.
 
Mark said:
We have a .Net VSTO Add-in for Outlook 2007.

We receive a version DateTime field from our ASP.NET webservice from a
server in Florida, e.g. dtServerDateTime in Eastern Time Zone DateTime.

How can I convert this DateTime variable to the user's local computer time
so I can display it on a form on their desktop? They may be in Brazil, the
UK or another location.



Dim tziEastern = TimeZoneInfo.FindSystemTimeZoneById( _
"Eastern Standard Time")
Dim tziLocal = TimeZoneInfo.Local
Dim EasternDateTime, LocalDateTime As DateTime

EasternDateTime = #9/9/2009 11:00:00 PM#

LocalDateTime = TimeZoneInfo.ConvertTime( _
EasternDateTime, tziEastern, tziLocal)

MsgBox(LocalDateTime)


General hint: If you're dealing with different time zones, you may
consider using the DateTimeOffset type instead of DateTime. In a global
context, if the DateTime's 'Kind' property is 'local' it's not clear
which locality is meant. (However, the ConvertTime function is clever
enough to throw an exception if the DateTime object's Kind property
value does not match the source time zone.) If you set the value by a
DateTime literal, it's Kind=Unspecified.
 
We have a .Net VSTO Add-in for Outlook 2007.

We receive a version DateTime field from our ASP.NET webservice from a
server in Florida, e.g. dtServerDateTime in Eastern Time Zone DateTime.

How can I convert this DateTime variable to the user's local computer time
so I can display it on a form on their desktop? They may be in Brazil, the
UK or another location.

I am assuming our shared server hosted by serverintellect.com doesn't adjust
for daylight savings too... If so I would need to take account of that and
the local user's daylight saving I am guessing.

If your using 3.5, then check out the TimeZoneInfo class. I believe it has
methods to convert from one timezone to another...
 
Thanks that worked but it was out by one hour:

- The Florida server returned "9/09/2009 6:15:51 pm" (EDT at that time was
actually 7:15:51 pm)
- The VSTO app here in New Zealand returned "10/09/2009 10:15:51 am"

However the time here in New Zealand was "11:15:51 am" not "10:15:51am".

'-------
Class DateTimeConvert

Public Function fToLocal(ByVal dtRawDateTime As DateTime) As
DateTime

'To convert server time value to local time
Dim tziEastern = TimeZoneInfo.FindSystemTimeZoneById("Eastern
Standard Time")
Dim tziLocal = TimeZoneInfo.Local
Dim LocalDateTime As DateTime = Nothing
LocalDateTime = TimeZoneInfo.ConvertTime(dtRawDateTime,
tziEastern, tziLocal)
Return LocalDateTime

End Function

End Class
 
Back
Top