RobertFromSecretWeapons
asked on
Determine Time Offset from New York
Hi,
I have an application that is distributed all over the world and therefore is run in a wide variety of time zones. I need a way to determine the difference in time (in hours) from where the application is run and New York.
For example, if the application is run in Vancouver Canada (which is -3hrs from NY) then I need a way to determine that difference so I can correctly display New York time from within the application.
I have an application that is distributed all over the world and therefore is run in a wide variety of time zones. I need a way to determine the difference in time (in hours) from where the application is run and New York.
For example, if the application is run in Vancouver Canada (which is -3hrs from NY) then I need a way to determine that difference so I can correctly display New York time from within the application.
Found this in another question: http://www.codeproject.com/KB/vb/TimeZoneInfo.aspx It's meant for changing time zones but it seems to collect all the relevant info first including daylight savings time.
Here is some code tio convert from your local time zone to Eastern Time.
To test, change the timezone on your computer to Pacific Time or any other zone and run.
To test, change the timezone on your computer to Pacific Time or any other zone and run.
string localZoneId = "Eastern Standard Time";
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(localZoneId);
DateTime localTime = DateTime.UtcNow;
Console.WriteLine(string.Format("Local Time: {0}",DateTime.Now.ToLongTimeString()));
DateTime easternTime = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Utc, tzi);
Console.WriteLine(string.Format("Eastern Time: {0}",easternTime.ToLongTimeString()));
And the VB version:
Dim localZoneId As String = "Eastern Standard Time"
Dim tzi As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(localZoneId)
Dim localTime As DateTime = DateTime.UtcNow
Console.WriteLine(String.Format("Local Time: {0}", DateTime.Now.ToLongTimeString()))
Dim easternTime As DateTime = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Utc, tzi)
Console.WriteLine(String.Format("Eastern Time: {0}", easternTime.ToLongTimeString()))
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
All three answers were quite good, but Ark addressed my question specifically.
Thx.
All three answers were quite good, but Ark addressed my question specifically.
Thx.
Thanks for points, Glad I could help.
More correct version below. In my short sample above there can be problem with daylight saving time - in the day of time switching GetUtcOffset parameter should refer to localTime (local for machine and NY_Time). For example? if tiime changes at 03 AM in NY and Vancouver time is 01 AM Utc offset will differ.
More correct version below. In my short sample above there can be problem with daylight saving time - in the day of time switching GetUtcOffset parameter should refer to localTime (local for machine and NY_Time). For example? if tiime changes at 03 AM in NY and Vancouver time is 01 AM Utc offset will differ.
Dim Local_Offset As Double = System.TimeZoneInfo.Local.GetUtcOffset(Date.Now).TotalHours
Dim Dim tzi_NY As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").
Dim NY_Time As DateTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.Utc, tzi_NY)
Dim NY_Offset As Double = tzi_NY.GetUtcOffset(NY_Tyme).TotalHours
Dim delta As Double = Local_Offset - NY_Offset
ASKER
Thx Ark, I would not have known that unless you provided this.
ASKER
Ark, Can you give a second look to what you just provided, you have a couple of typos in it (beginning and end of line 2). Pls clarify..
Sorry, I wrote above code directly here, didn't check it in IDE.
Correct version:
Correct version:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Local_Offset As Double = System.TimeZoneInfo.Local.GetUtcOffset(Date.Now).TotalHours
Dim tzi_NY As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
Dim NY_Time As DateTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.Utc, tzi_NY)
Dim NY_Offset As Double = tzi_NY.GetUtcOffset(NY_Time).TotalHours
Dim delta As Double = Local_Offset - NY_Offset
MsgBox(delta)
End Sub
A bit better formatted:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Local_Offset As Double = System.TimeZoneInfo.Local.GetUtcOffset(Date.Now).TotalHours
Dim tzi_NY As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
Dim NY_Time As DateTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.Utc, tzi_NY)
Dim NY_Offset As Double = tzi_NY.GetUtcOffset(NY_Time).TotalHours
Dim delta As Double = Local_Offset - NY_Offset
Dim sFormat As String = "UTC {0};" & vbCrLf
sFormat += "Local time: {1};" & vbCrLf
sFormat += "NY_Time: {2};" & vbCrLf
sFormat += "Local offset: {3};" & vbCrLf
sFormat += "NY_Offset: {4}; " & vbCrLf
sFormat += "delta: {5}" & vbCrLf
MsgBox(String.Format(sFormat, DateTime.UtcNow, DateTime.Now, NY_Time, Local_Offset, NY_Offset, delta))
End Sub
ASKER
Thx for the clarification. It was the dot on the end of the second line that made me wonder...