Edit

Share via


TimeZoneInfo.ClearCachedData Method

Definition

Clears cached time zone data.

C#
public static void ClearCachedData();

Remarks

Cached time zone data includes data on the local time zone and the Coordinated Universal Time (UTC) zone.

You might call the ClearCachedData method to reduce the memory devoted to the application's cache of time zone information or to reflect the fact that the local system's time zone has changed.

Storing references to the local and UTC time zones is not recommended. After the call to the ClearCachedData method, these object variables will be undefined TimeZoneInfo objects that are no longer references to TimeZoneInfo.Local or TimeZoneInfo.Utc. For example, in the following code, the second call to the TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) method throws an ArgumentException because the local variable is no longer considered equal to TimeZoneInfo.Local.

C#
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
TimeZoneInfo local = TimeZoneInfo.Local;
Console.WriteLine(TimeZoneInfo.ConvertTime(DateTime.Now, local, cst));

TimeZoneInfo.ClearCachedData();
try
{
   Console.WriteLine(TimeZoneInfo.ConvertTime(DateTime.Now, local, cst));
}
catch (ArgumentException e)
{
   Console.WriteLine(e.GetType().Name + "\n   " + e.Message);
}

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1