How to represent the current UK time?

前端 未结 5 1868
暗喜
暗喜 2021-01-02 02:37

I\'m facing an issue while converting dates between my server and client where both is running in Germany. The Regional settings on the client machines could be set to both

5条回答
  •  梦谈多话
    2021-01-02 03:40

    Use TimeZoneInfo.ConvertTime to convert original input timezone (CET) to target timezone (UK).

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

    Full guidance on MSDN here:

    Converting Times Between Time Zones

    Modified code sample 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.");
    }
    

提交回复
热议问题