Question:

C Sharp Time Zone Conversion

by Guest32143  |  earlier

0 LIKES UnLike

I'm reading a datafeed from Betfair.

The Timezone field is in UKT timezone.

I am in London timezone which is one hour different.

How can I convert the time to the new timezone?

I want a solution more elegant than merely adding an hour to the feed timezone as that method will fail if the timezone changes, or when the clocks change in winter.

 Tags:

   Report

2 ANSWERS


  1. DateTime timeUtc = DateTime.UtcNow;

    try

    {

       TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Cen... Standard Time");

       DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);

       Console.WriteLine("The date and time are {0} {1}.",

                         cstTime,

                         cstZone.IsDaylightSavingTime(cstTime) ?

                                 cstZone.DaylightName : cstZone.StandardName);

    }

    catch (TimeZoneNotFoundException)

    {

       Console.WriteLine("The registry does not define the Central Standard Time zone.");

    }                          

    catch (InvalidTimeZoneException)

    {

       Console.WriteLine("Registry data on the Central STandard Time zone has been corrupted.");

    }


  2. what application are you using?

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.