Converts a time to the time in a particular time zone. : TimeZoneInfo « Date Time « C# / C Sharp






Converts a time to the time in a particular time zone.

 

using System;

public class Example
{
   public static void Main()
   {
      DateTime[] times = { new DateTime(2010, 1, 1, 0, 1, 0), 
                           new DateTime(2010, 1, 1, 0, 1, 0, DateTimeKind.Utc), 
                           new DateTime(2010, 1, 1, 0, 1, 0, DateTimeKind.Local)};

      TimeZoneInfo est; 
      try {
         est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
      }
      catch (TimeZoneNotFoundException) {
         Console.WriteLine("Unable to retrieve the Eastern Standard time zone.");
         return;
      }
      catch (InvalidTimeZoneException) {
         Console.WriteLine("Unable to retrieve the Eastern Standard time zone.");
         return;
      }   

      Console.WriteLine("Local time zone: {0}\n", TimeZoneInfo.Local.DisplayName);

      foreach (DateTime timeToConvert in times)
      {
         DateTime targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est);
         Console.WriteLine("Converted {0} {1} to {2}.", timeToConvert, timeToConvert.Kind, targetTime);
      }                        
   }
}

   
  








Related examples in the same category

1.The Id property corresponds to the value passed to FindSystemTimeZoneById.
2.The TimeZoneInfo class works in a similar manner. TimeZoneInfo.Local returns the current local time zone:
3.TimeZoneInfo also provides IsDaylightSavingTime and GetUtcOffset methods--the difference is that they accept either a DateTime or DateTimeOffset.
4.Week refers to the week of the month, with "5" meaning the last week.
5.Gets the time difference between the current time zone's standard time and Coordinated Universal Time (UTC).
6.Converts a time from one time zone to another.
7.Converts a time from one time zone to another based on time zone identifiers.
8.Converts a Coordinated Universal Time (UTC) to the time in a specified time zone.
9.Converts the current date and time to Coordinated Universal Time (UTC).
10.Converts time in a specified time zone to Coordinated Universal Time (UTC).
11.Gets the localized general display name that represents the time zone.
12.Determines whether the current TimeZoneInfo object and another TimeZoneInfo object are equal.
13.Retrieves a TimeZoneInfo object from the registry based on its identifier.
14.Gets the time zone identifier.
15.whether a date and time in a time zone is ambiguous and can be mapped to two or more Coordinated Universal Time (UTC) times.
16.Whether a date and time falls in the range of daylight saving time
17.Gets a TimeZoneInfo object that represents the local time zone.
18.Get TimeZone Id, Display name, Standard name, and Daylight Name
19.Gets the localized display name for the time zone's standard time.
20.Gets a value indicating whether the time zone has any daylight saving time rules.
21.Gets a TimeZoneInfo object that represents the Coordinated Universal Time (UTC) zone.