C# TimeZoneNotFoundException
Program.cs
var tz = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById("IndiaIndia Standard Time"));
Console.WriteLine(tz);
Output
kodingwindow@kw:~/csharp$ dotnet run
Unhandled exception. System.TimeZoneNotFoundException: The time zone ID 'IndiaIndia Standard Time' was not found on the local computer.
    ---> System.TimeZoneNotFoundException: The time zone ID 'IndiaIndia Standard Time' is invalid.
      --- End of inner exception stack trace ---
      at System.TimeZoneInfo.FindSystemTimeZoneById(String id)
      at Program.<Main>$(String[] args) in /home/kodingwindow/csharp/Program.cs:line 1
kodingwindow@kw:~/csharp$ 
C# program to handle the TimeZoneNotFoundException
Program.cs
try
{
    var ctz = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById("IndiaIndia Standard Time"));
    Console.WriteLine(ctz);
}
catch(TimeZoneNotFoundException e)
{
    Console.WriteLine(e.Message);
    
    Console.WriteLine("\nList of correct time zone IDs");
    var tz = TimeZoneInfo.GetSystemTimeZones(); 
    foreach(TimeZoneInfo tzi in tz)
    {
        Console.WriteLine(tzi.Id);
    }
}
Output
kodingwindow@kw:~/csharp$ dotnet run
The time zone ID 'IndiaIndia Standard Time' was not found on the local computer.

List of correct time zone IDs
Dateline Standard Time
Hawaiian Standard Time
Marquesas Standard Time
Alaskan Standard Time
...
Pacific/Fakaofo
Pacific/Tongatapu
Pacific/Kiritimati
kodingwindow@kw:~/csharp$ 
Advertisement