Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ToSerializedString Method

TimeZoneInfo.ToSerializedString Method

Converts the current TimeZoneInfo object to a serialized string.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public string ToSerializedString()

Return Value

Type: System.String
A string that represents the current TimeZoneInfo object.

Applications that rely on time zones that are not typically defined in the registry of Windows systems can use the CreateCustomTimeZone method to instantiate the necessary time zones as TimeZoneInfo objects. The application can then call the ToSerializedString method to convert the time zone object to a string.

The TimeZoneInfo object should also be stored in a location where the application can retrieve it when needed. Possible locations include:

  • The registry.

  • An application resource file.

  • An external file, such as a text file.

The following example tries to retrieve the Antarctica/South Pole time zone from the local system. If it fails, the code tries to retrieve information about the time zone from a text file in the application directory. If this attempt fails, the code creates the time zone and writes information about it to the text file.

private TimeZoneInfo InitializeTimeZone()
   TimeZoneInfo southPole = null;
   // Determine if South Pole time zone is defined in system 
      southPole = TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");
   // Time zone does not exist; create it, store it in a text file, and return it 
      const string filename = @".\TimeZoneInfo.txt";
      bool found = false;

      if (File.Exists(filename))
         StreamReader reader = new StreamReader(filename);
         string timeZoneInfo;
         while (reader.Peek() >= 0)
            timeZoneInfo = reader.ReadLine();
            if (timeZoneInfo.Contains("Antarctica/South Pole"))
               southPole = TimeZoneInfo.FromSerializedString(timeZoneInfo);
               found = true;
      if (! found)
         // Define transition times to/from DST
         TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 10, 1, DayOfWeek.Sunday); 
         TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 3, 3, DayOfWeek.Sunday);
         // Define adjustment rule
         TimeSpan delta = new TimeSpan(1, 0, 0);
         TimeZoneInfo.AdjustmentRule adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1989, 10, 1), DateTime.MaxValue.Date, delta, startTransition, endTransition);
         // Create array for adjustment rules
         TimeZoneInfo.AdjustmentRule[] adjustments = {adjustment};
         // Define other custom time zone arguments 
         string displayName = "(GMT+12:00) Antarctica/South Pole";
         string standardName = "Antarctica/South Pole Standard Time";
         string daylightName = "Antarctica/South Pole Daylight Time";
         TimeSpan offset = new TimeSpan(12, 0, 0);
         southPole = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments);
         // Write time zone to the file
         StreamWriter writer = new StreamWriter(filename, true);
   return southPole;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft