[ACCEPTED]-How to convert time between timezones (UTC to EDT)?-timezone
Eastern Daylight Time isn't the name of 12 a "full" time zone - it's "half" a 11 time zone, effectively, always 4 hours behind 10 UTC. (There may be proper terminology for 9 this, but I'm not aware of it.)
Why would 8 you want to use EDT for times which don't 7 have daylight savings applied? If you want 6 a custom time zone that always has the same 5 offset to UTC, use
Note that if you use 4 get the Eastern Standard timezone (
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")) then 3 that will still have daylight saving time 2 applied appropriately (i.e. during summer).
For 1 example:
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); // Prints True Console.WriteLine(tzi.IsDaylightSavingTime(new DateTime(2009, 6, 1))); // Prints False Console.WriteLine(tzi.IsDaylightSavingTime(new DateTime(2009, 1, 1)));
I would have said that you should use UTC 41 for calculations of time periods, so that 40 you avoid issues of daylight saving time 39 and then use LocalTime for display only.
DateTime.ToLocalTime 38 for UTC to whatever the local time zone 37 is and then DateTime.ToUniversalTime to 36 convert from local time to UTC.
Edit after comment 1
Do I take 35 it then that you're after displaying a different 34 timezone to that of the server?
If you're 33 using web pages to access your server then 32 use HttpRequest.UserLanguages to help create 31 a CultureInfo object and use that to parse 30 your DateTime object. Look here for a full 29 explanation:Microsoft link on displaying local user time for web pages.
If you're using client-server 28 architecture then if the LocalTime call 27 is on the client side it will display the 26 LocalTime for the client. You then convert 25 it to UTC to send back to your server.
Either 24 way your server doesn't need to know where 23 the client is so if you have multiple clients 22 in multiple timezones then all calculations 21 will match. It will also allow you to show 20 the times in any timezone that you wish 19 by use of different Culture objects.
Edit 2 copied my second comment
You 18 can get time data in UTC format from the 17 server. Then you can convert it using DateTime.ToLocalTime 16 or DateTime.ToUniversalTime as requried. If 15 you're including dates as well and need 14 to cope with say US MM/dd/yyyy and european 13 dd/MM/yyyy formats the you can use CultureInfo 12 class to parse the DateTime value accordingly. It 11 sounds like more work than what you have 10 at the moment, but it would mean that if 9 you move your server again then you don't 8 need to recode the DateTime handling.
A new point
Another 7 point to look at is clock synchronisation 6 between the server and the clients using 5 NTP (Network Time Protocol) or SNTP (Simple 4 Network Time Protocol) if it is accurate 3 enough. I don't know what OS you are using 2 but this is used by Windows Server time 1 services to synchronise networks.
The cowboy method is to take the UTC time, subtract 7 four hours' worth of seconds from it (the 6 timezone offset), format it using a UTC 5 formatting function, and slap a "EDT" label 4 on it.
If you need to use Daylight Time sometimes 3 and Standard Time other times, either make 2 a lookup table of switchover dates, or use 1 some calendar function.
TimeZoneInfo.ConvertTimeFromUtc will have 4 correct offset depending on the DateTime 3 you give it. For example:
3AM UTC/11PM ET 2 (4 hour offset):
DateTime timeSummerET = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime("08/01/2019 03:00:00"), zoneET);
3AM UTC/10PM ET (5 hour 1 offset):
DateTime timeWinterET = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime("12/01/2019 03:00:00"), zoneET);
More Related questions