TimeZone Not Found Exception [duplicate]

2019-07-14 03:30发布

This question already has an answer here:

I want to convert Indian DateTime object to Eastern DateTime object. Means I want to change time zone of particular DateTime object. For this I have written following code:

string easternZoneId = "Eastern Standard Time";
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById(easternZoneId);

string indianZoneId = "India Standard Time";
TimeZoneInfo indianZone = TimeZoneInfo.FindSystemTimeZoneById (indianZoneId);

DateTime d = TimeZoneInfo.ConvertTime (DateTime.Today, indianZone, easternZone);

When I try to run this statement I am getting, following exception in Console.

enter image description here

How to run this code? I want to convert my time into eastern time.

EDIT: Right now I am running my code in Unity Editor. I have iMac system. I want to run this code for iPhone devices.

2条回答
等我变得足够好
2楼-- · 2019-07-14 04:09

"India Standard Time" does not exist on my PC. You can list all the supported timezones on your PC (I have noticed that this can be different on other PC's) by listing them like this:

var timeZones = TimeZoneInfo.GetSystemTimeZones(); 
foreach (TimeZoneInfo timeZone in timeZones)
{
    Console.WriteLine(timeZone.Id);
}

Out:

  • Dateline Standard Time
  • UTC-11
  • Hawaiian Standard Time
  • ...
查看更多
闹够了就滚
3楼-- · 2019-07-14 04:18

From msdn https://msdn.microsoft.com/en-us/library/system.timezoneinfo.findsystemtimezonebyid(v=vs.110).aspxenter link description here

FindSystemTimeZoneById tries to match id to the subkey names of the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones branch of the registry under Windows XP and Windows Vista. This branch does not necessarily contain a comprehensive list of time zone identifiers. If required by an application, you can create a particular time zone either by calling one of the overloads of the CreateCustomTimeZone method or by calling FromSerializedString to deserialize a TimeZoneInfo object that represents the required time zone. However, time zones created by these method calls are not included in the registry and cannot be retrieved using the FindSystemTimeZoneById method. These custom time zones can be accessed only through the object reference returned by the CreateCustomTimeZone or FromSerializedString method call.

Example of method usage:

 // Get Tokyo Standard Time zone
      TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
      DateTime tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst);      
      Console.WriteLine("Time in {0} zone: {1}", tst.IsDaylightSavingTime(tstTime) ?
                        tst.DaylightName : tst.StandardName, tstTime);
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst));

List of timezones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Just find your timezone id and insert in the example.

查看更多
登录 后发表回答