TimeZone Not Found Exception [duplicate]

假装没事ソ 提交于 2020-01-04 13:36:57

问题


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.

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.


回答1:


"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
  • ...



回答2:


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.



来源:https://stackoverflow.com/questions/36838663/timezone-not-found-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!