How to present the current time of the UK?

I had a problem converting dates between my server and client, where both work in Germany. Regional settings on client machines can be set in both the UK and Germany. I get the date from the server, which is the CET format, and I need to present this time in the user interface as UK time. For example, the time received from the server, for example, 01/07/2010 01:00:00, should be presented in the user interface 01/07/2010 00:00:00. I wrote a converter for this purpose, but during operation it gets a time difference of 2 hours. Below is the code, please can you help?

public class LocalToGmtConverter : IDateConverter { private readonly TimeZoneInfo timeZoneInfo; public LocalToGmtConverter() : this(TimeZoneInfo.Local) { } public LocalToGmtConverter(TimeZoneInfo timeZoneInfo) { this.timeZoneInfo = timeZoneInfo; } public DateTime Convert(DateTime localDate) { var utcKind = DateTime.SpecifyKind(localDate, DateTimeKind.Utc); return utcKind; } public DateTime ConvertBack(object fromServer) { DateTime serverDate = (DateTime)fromServer; var utcOffset = timeZoneInfo.GetUtcOffset(serverDate); var uiTime = serverDate- utcOffset; return uiTime; } } 
+5
c # datetime
Oct 27 '10 at 15:29
source share
5 answers

I think you are switching to UTC (and not to the UK). Since Central Europe still has daylight saving time (an event, if the temperature says otherwise), the difference is +2 hours until October 31.

If you know that you are moving from Germany to the UK (i.e. CEST in BST in summer and CET in GMT in winter), why don't you just subtract 1 hour?

If you need time zone information for the UK, you can build it using

 var britishZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); 

Then you can convert the date using

 var newDate = TimeZoneInfo.ConvertTime(serverDate, TimeZoneInfo.Local, britishZone); 
+12
Oct 27 2018-10-27
source share

Use TimeZoneInfo.ConvertTime to convert the source time zone (CET) to the target time zone (UK).

 public static DateTime ConvertTime( DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone ) 

The complete MSDN guide is here :

Convert time between time zones

Changed example code from MSDN:

 DateTime ceTime = new DateTime(2007, 02, 01, 08, 00, 00); try { TimeZoneInfo ceZone = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time"); TimeZoneInfo gmtZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); Console.WriteLine("{0} {1} is {2} GMT time.", ceTime, ceZone.IsDaylightSavingTime(ceTime) ? ceZone.DaylightName : ceZone.StandardName, TimeZoneInfo.ConvertTime(ceTime, ceZone, gmtZone)); } catch (TimeZoneNotFoundException) { Console.WriteLine("The registry does not define the required timezones."); } catch (InvalidTimeZoneException) { Console.WriteLine("Registry data on the required timezones has been corrupted."); } 
+1
Oct 27 '10 at 15:35
source share

The best approach to local times is to store them in a unified view such as UTC.

Thus, you can convert all input time to UTC (via .ToUniversalTime() ) and save (or transfer) its value. When you need to show it, just convert it back using .ToLocalTime() .

This way you avoid rquirements to find out which time zone was the original, and can easily display the stored value in different time zones.

You can also avoid problems when you need to write some logic for processing time in the next time zone, trying to figure out how to convert them among all available.

0
Oct 27 '10 at 16:05
source share

This is what I do:

  var BritishZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified); DateTimeInBritishLocal = TimeZoneInfo.ConvertTime(dt, TimeZoneInfo.Local, BritishZone); 

I needed to add a part to SpecifyKind or ConvertTime throws an exception

0
Mar 27 '18 at 8:59
source share

I understand that the question relates to C #, but if all you want to do is one conversion, you can do it from the PowerShell command line:

 $d = [DateTime]::Parse("04/02/2014 17:00:00") $gmt = [TimeZoneInfo]::FindSystemTimeZoneById("GMT Standard Time"); [TimeZoneInfo]::ConvertTime($d, $gmt, [TimeZoneInfo]::Local) 

This script will convert 17:00 UK time to your local time zone. For me it will be CST. It is interesting to note that if I set the date before March 27, 2012, the result would be different, because the time for daylight saving time in the UK lasts on different dates, in the USA.

-2
Mar 27 '14 at 17:31
source share



All Articles