System.TimeZoneNotFoundException error while getting datetime for particular time zone in c#
I didn't solved this issue but I have a work around for this scenario using https://stackoverflow.com/a/6144229/7670060. Hope this will help.
Step1:
Find the list of time zone in your computer.
var timeZones = TimeZoneInfo.GetSystemTimeZones();
Step2:
public string OlsonTimeZoneToTimeZoneInfo(string timeInfo)
{
var olsonWindowsTimes = new Dictionary<string, string>()
{
{ "Africa/Bangui", "W. Central Africa Standard Time" },
{ "Africa/Cairo", "Egypt Standard Time" },
{ "Africa/Casablanca", "Morocco Standard Time" },
{ "Africa/Harare", "South Africa Standard Time" },
{ "Africa/Johannesburg", "South Africa Standard Time" },
{ "Africa/Lagos", "W. Central Africa Standard Time" },
{ "Africa/Monrovia", "Greenwich Standard Time" },
{ "Africa/Nairobi", "E. Africa Standard Time" },
{ "Africa/Windhoek", "Namibia Standard Time" },
{ "America/Anchorage", "Alaskan Standard Time" },
{ "America/Argentina/San_Juan", "Argentina Standard Time" },
{ "America/Asuncion", "Paraguay Standard Time" },
{ "America/Bahia", "Bahia Standard Time" },
{ "America/Bogota", "SA Pacific Standard Time" },
{ "America/Buenos_Aires", "Argentina Standard Time" },
{ "America/Caracas", "Venezuela Standard Time" },
{ "America/Cayenne", "SA Eastern Standard Time" },
{ "America/Chicago", "Central Standard Time" },
{ "America/Chihuahua", "Mountain Standard Time (Mexico)" },
{ "America/Cuiaba", "Central Brazilian Standard Time" },
{ "America/Denver", "Mountain Standard Time" },
{ "America/Fortaleza", "SA Eastern Standard Time" },
{ "America/Godthab", "Greenland Standard Time" },
{ "America/Guatemala", "Central America Standard Time" },
{ "America/Halifax", "Atlantic Standard Time" },
{ "America/Indianapolis", "US Eastern Standard Time" },
{ "America/Indiana/Indianapolis", "US Eastern Standard Time" },
{ "America/La_Paz", "SA Western Standard Time" },
{ "America/Los_Angeles", "Pacific Standard Time" },
{ "America/Mexico_City", "Mexico Standard Time" },
{ "America/Montevideo", "Montevideo Standard Time" },
{ "America/New_York", "Eastern Standard Time" },
{ "America/Noronha", "UTC-02" },
{ "America/Phoenix", "US Mountain Standard Time" },
{ "America/Regina", "Canada Central Standard Time" },
{ "America/Santa_Isabel", "Pacific Standard Time (Mexico)" },
{ "America/Santiago", "Pacific SA Standard Time" },
{ "America/Sao_Paulo", "E. South America Standard Time" },
{ "America/St_Johns", "Newfoundland Standard Time" },
{ "America/Tijuana", "Pacific Standard Time" },
{ "Antarctica/McMurdo", "New Zealand Standard Time" },
{ "Atlantic/South_Georgia", "UTC-02" },
{ "Asia/Almaty", "Central Asia Standard Time" },
{ "Asia/Amman", "Jordan Standard Time" },
{ "Asia/Baghdad", "Arabic Standard Time" },
{ "Asia/Baku", "Azerbaijan Standard Time" },
{ "Asia/Bangkok", "SE Asia Standard Time" },
{ "Asia/Beirut", "Middle East Standard Time" },
{ "Asia/Calcutta", "India Standard Time" },
{ "Asia/Colombo", "Sri Lanka Standard Time" },
{ "Asia/Damascus", "Syria Standard Time" },
{ "Asia/Dhaka", "Bangladesh Standard Time" },
{ "Asia/Dubai", "Arabian Standard Time" },
{ "Asia/Irkutsk", "North Asia East Standard Time" },
{ "Asia/Jerusalem", "Israel Standard Time" },
{ "Asia/Kabul", "Afghanistan Standard Time" },
{ "Asia/Kamchatka", "Kamchatka Standard Time" },
{ "Asia/Karachi", "Pakistan Standard Time" },
{ "Asia/Katmandu", "Nepal Standard Time" },
{ "Asia/Kolkata", "India Standard Time" },
{ "Asia/Krasnoyarsk", "North Asia Standard Time" },
{ "Asia/Kuala_Lumpur", "Singapore Standard Time" },
{ "Asia/Kuwait", "Arab Standard Time" },
{ "Asia/Magadan", "Magadan Standard Time" },
{ "Asia/Muscat", "Arabian Standard Time" },
{ "Asia/Novosibirsk", "N. Central Asia Standard Time" },
{ "Asia/Oral", "West Asia Standard Time" },
{ "Asia/Rangoon", "Myanmar Standard Time" },
{ "Asia/Riyadh", "Arab Standard Time" },
{ "Asia/Seoul", "Korea Standard Time" },
{ "Asia/Shanghai", "China Standard Time" },
{ "Asia/Singapore", "Singapore Standard Time" },
{ "Asia/Taipei", "Taipei Standard Time" },
{ "Asia/Tashkent", "West Asia Standard Time" },
{ "Asia/Tbilisi", "Georgian Standard Time" },
{ "Asia/Tehran", "Iran Standard Time" },
{ "Asia/Tokyo", "Tokyo Standard Time" },
{ "Asia/Ulaanbaatar", "Ulaanbaatar Standard Time" },
{ "Asia/Vladivostok", "Vladivostok Standard Time" },
{ "Asia/Yakutsk", "Yakutsk Standard Time" },
{ "Asia/Yekaterinburg", "Ekaterinburg Standard Time" },
{ "Asia/Yerevan", "Armenian Standard Time" },
{ "Atlantic/Azores", "Azores Standard Time" },
{ "Atlantic/Cape_Verde", "Cape Verde Standard Time" },
{ "Atlantic/Reykjavik", "Greenwich Standard Time" },
{ "Australia/Adelaide", "Cen. Australia Standard Time" },
{ "Australia/Brisbane", "E. Australia Standard Time" },
{ "Australia/Darwin", "AUS Central Standard Time" },
{ "Australia/Hobart", "Tasmania Standard Time" },
{ "Australia/Perth", "W. Australia Standard Time" },
{ "Australia/Sydney", "AUS Eastern Standard Time" },
{ "Etc/GMT", "UTC" },
{ "Etc/GMT+11", "UTC-11" },
{ "Etc/GMT+12", "Dateline Standard Time" },
{ "Etc/GMT+2", "UTC-02" },
{ "Etc/GMT-12", "UTC+12" },
{ "Europe/Amsterdam", "W. Europe Standard Time" },
{ "Europe/Athens", "GTB Standard Time" },
{ "Europe/Belgrade", "Central Europe Standard Time" },
{ "Europe/Berlin", "W. Europe Standard Time" },
{ "Europe/Brussels", "Romance Standard Time" },
{ "Europe/Budapest", "Central Europe Standard Time" },
{ "Europe/Dublin", "GMT Standard Time" },
{ "Europe/Helsinki", "FLE Standard Time" },
{ "Europe/Istanbul", "GTB Standard Time" },
{ "Europe/Kiev", "FLE Standard Time" },
{ "Europe/London", "GMT Standard Time" },
{ "Europe/Minsk", "E. Europe Standard Time" },
{ "Europe/Moscow", "Russian Standard Time" },
{ "Europe/Paris", "Romance Standard Time" },
{ "Europe/Sarajevo", "Central European Standard Time" },
{ "Europe/Warsaw", "Central European Standard Time" },
{ "Indian/Mauritius", "Mauritius Standard Time" },
{ "Pacific/Apia", "Samoa Standard Time" },
{ "Pacific/Auckland", "New Zealand Standard Time" },
{ "Pacific/Fiji", "Fiji Standard Time" },
{ "Pacific/Guadalcanal", "Central Pacific Standard Time" },
{ "Pacific/Guam", "West Pacific Standard Time" },
{ "Pacific/Honolulu", "Hawaiian Standard Time" },
{ "Pacific/Pago_Pago", "UTC-11" },
{ "Pacific/Port_Moresby", "West Pacific Standard Time" },
{ "Pacific/Tongatapu", "Tonga Standard Time" }
};
string timeInfoKey = string.Empty;
if (olsonWindowsTimes.ContainsValue(timeInfo))
timeInfoKey = olsonWindowsTimes.FirstOrDefault(x => x.Value == timeInfo).Key;
return timeInfoKey;
}
Step3:
var finalResult=ConvertTimeZone(Datetime.UtcNow,"Central Standard Time");
public DateTime ConvertTimeZone(DateTime dateTime, string timeInfo)
{
var result = OlsonTimeZoneToTimeZoneInfo(timeInfo);
if (result != null)
{
var finalTimeZone = TimeZoneInfo.FindSystemTimeZoneById(result);
dateTime = (dateTime != null ? TimeZoneInfo.ConvertTimeFromUtc(dateTime != null ? dateTime : DateTime.UtcNow, finalTimeZone) : DateTime.UtcNow);
}
return dateTime;
}
Assuming the time zone identifiers in your file came from Rails, then you are basically asking the reverse of this question.
Similar to the answer of that question, we will use the Rails mapping, along with CLDR mappings, to obtain a Rails-To-Windows mapping constant in C#.
public static class TZConvert
{
public static string RailsToWindows(string railsTimeZoneId)
{
return RailsWindowsMapping.ContainsKey(railsTimeZoneId)
? RailsWindowsMapping[railsTimeZoneId]
: null;
}
private static readonly IReadOnlyDictionary<string, string> RailsWindowsMapping =
new Dictionary<string, string>
{
{"Abu Dhabi", "Arabian Standard Time"},
{"Adelaide", "Cen. Australia Standard Time"},
{"Alaska", "Alaskan Standard Time"},
{"Almaty", "Central Asia Standard Time"},
{"American Samoa", "UTC-11"},
{"Amsterdam", "W. Europe Standard Time"},
{"Arizona", "US Mountain Standard Time"},
{"Astana", "Bangladesh Standard Time"},
{"Athens", "GTB Standard Time"},
{"Atlantic Time (Canada)", "Atlantic Standard Time"},
{"Auckland", "New Zealand Standard Time"},
{"Azores", "Azores Standard Time"},
{"Baghdad", "Arabic Standard Time"},
{"Baku", "Azerbaijan Standard Time"},
{"Bangkok", "SE Asia Standard Time"},
{"Beijing", "China Standard Time"},
{"Belgrade", "Central Europe Standard Time"},
{"Berlin", "W. Europe Standard Time"},
{"Bern", "W. Europe Standard Time"},
{"Bogota", "SA Pacific Standard Time"},
{"Brasilia", "E. South America Standard Time"},
{"Bratislava", "Central Europe Standard Time"},
{"Brisbane", "E. Australia Standard Time"},
{"Brussels", "Romance Standard Time"},
{"Bucharest", "GTB Standard Time"},
{"Budapest", "Central Europe Standard Time"},
{"Buenos Aires", "Argentina Standard Time"},
{"Cairo", "Egypt Standard Time"},
{"Canberra", "AUS Eastern Standard Time"},
{"Cape Verde Is.", "Cape Verde Standard Time"},
{"Caracas", "Venezuela Standard Time"},
{"Casablanca", "Morocco Standard Time"},
{"Central America", "Central America Standard Time"},
{"Central Time (US & Canada)", "Central Standard Time"},
{"Chennai", "India Standard Time"},
{"Chihuahua", "Mountain Standard Time (Mexico)"},
{"Chongqing", "China Standard Time"},
{"Copenhagen", "Romance Standard Time"},
{"Darwin", "AUS Central Standard Time"},
{"Dhaka", "Bangladesh Standard Time"},
{"Dublin", "GMT Standard Time"},
{"Eastern Time (US & Canada)", "Eastern Standard Time"},
{"Edinburgh", "GMT Standard Time"},
{"Ekaterinburg", "Ekaterinburg Standard Time"},
{"Fiji", "Fiji Standard Time"},
{"Georgetown", "SA Western Standard Time"},
{"Greenland", "Greenland Standard Time"},
{"Guadalajara", "Central Standard Time (Mexico)"},
{"Guam", "West Pacific Standard Time"},
{"Hanoi", "SE Asia Standard Time"},
{"Harare", "South Africa Standard Time"},
{"Hawaii", "Hawaiian Standard Time"},
{"Helsinki", "FLE Standard Time"},
{"Hobart", "Tasmania Standard Time"},
{"Hong Kong", "China Standard Time"},
{"Indiana (East)", "US Eastern Standard Time"},
{"International Date Line West", "UTC-11"},
{"Irkutsk", "North Asia East Standard Time"},
{"Islamabad", "Pakistan Standard Time"},
{"Istanbul", "Turkey Standard Time"},
{"Jakarta", "SE Asia Standard Time"},
{"Jerusalem", "Israel Standard Time"},
{"Kabul", "Afghanistan Standard Time"},
{"Kaliningrad", "Kaliningrad Standard Time"},
{"Kamchatka", "Russia Time Zone 11"},
{"Karachi", "Pakistan Standard Time"},
{"Kathmandu", "Nepal Standard Time"},
{"Kolkata", "India Standard Time"},
{"Krasnoyarsk", "North Asia Standard Time"},
{"Kuala Lumpur", "Singapore Standard Time"},
{"Kuwait", "Arab Standard Time"},
{"Kyiv", "FLE Standard Time"},
{"La Paz", "SA Western Standard Time"},
{"Lima", "SA Pacific Standard Time"},
{"Lisbon", "GMT Standard Time"},
{"Ljubljana", "Central Europe Standard Time"},
{"London", "GMT Standard Time"},
{"Madrid", "Romance Standard Time"},
{"Magadan", "Magadan Standard Time"},
{"Marshall Is.", "UTC+12"},
{"Mazatlan", "Mountain Standard Time (Mexico)"},
{"Melbourne", "AUS Eastern Standard Time"},
{"Mexico City", "Central Standard Time (Mexico)"},
{"Mid-Atlantic", "UTC-02"},
{"Midway Island", "UTC-11"},
{"Minsk", "Belarus Standard Time"},
{"Monrovia", "Greenwich Standard Time"},
{"Monterrey", "Central Standard Time (Mexico)"},
{"Montevideo", "Montevideo Standard Time"},
{"Moscow", "Russian Standard Time"},
{"Mountain Time (US & Canada)", "Mountain Standard Time"},
{"Mumbai", "India Standard Time"},
{"Muscat", "Arabian Standard Time"},
{"Nairobi", "E. Africa Standard Time"},
{"New Caledonia", "Central Pacific Standard Time"},
{"New Delhi", "India Standard Time"},
{"Newfoundland", "Newfoundland Standard Time"},
{"Novosibirsk", "N. Central Asia Standard Time"},
{"Nuku'alofa", "Tonga Standard Time"},
{"Osaka", "Tokyo Standard Time"},
{"Pacific Time (US & Canada)", "Pacific Standard Time"},
{"Paris", "Romance Standard Time"},
{"Perth", "W. Australia Standard Time"},
{"Port Moresby", "West Pacific Standard Time"},
{"Prague", "Central Europe Standard Time"},
{"Pretoria", "South Africa Standard Time"},
{"Quito", "SA Pacific Standard Time"},
{"Rangoon", "Myanmar Standard Time"},
{"Riga", "FLE Standard Time"},
{"Riyadh", "Arab Standard Time"},
{"Rome", "W. Europe Standard Time"},
{"Samara", "Russia Time Zone 3"},
{"Samoa", "Samoa Standard Time"},
{"Santiago", "Pacific SA Standard Time"},
{"Sapporo", "Tokyo Standard Time"},
{"Sarajevo", "Central European Standard Time"},
{"Saskatchewan", "Canada Central Standard Time"},
{"Seoul", "Korea Standard Time"},
{"Singapore", "Singapore Standard Time"},
{"Skopje", "Central European Standard Time"},
{"Sofia", "FLE Standard Time"},
{"Solomon Is.", "Central Pacific Standard Time"},
{"Srednekolymsk", "Russia Time Zone 10"},
{"Sri Jayawardenepura", "Sri Lanka Standard Time"},
{"St. Petersburg", "Russian Standard Time"},
{"Stockholm", "W. Europe Standard Time"},
{"Sydney", "AUS Eastern Standard Time"},
{"Taipei", "Taipei Standard Time"},
{"Tallinn", "FLE Standard Time"},
{"Tashkent", "West Asia Standard Time"},
{"Tbilisi", "Georgian Standard Time"},
{"Tehran", "Iran Standard Time"},
{"Tijuana", "Pacific Standard Time"},
{"Tokelau Is.", "Tonga Standard Time"},
{"Tokyo", "Tokyo Standard Time"},
{"Ulaanbaatar", "Ulaanbaatar Standard Time"},
{"Urumqi", "Central Asia Standard Time"},
{"UTC", "UTC"},
{"Vienna", "W. Europe Standard Time"},
{"Vilnius", "FLE Standard Time"},
{"Vladivostok", "Vladivostok Standard Time"},
{"Volgograd", "Russian Standard Time"},
{"Warsaw", "Central European Standard Time"},
{"Wellington", "New Zealand Standard Time"},
{"West Central Africa", "W. Central Africa Standard Time"},
{"Yakutsk", "Yakutsk Standard Time"},
{"Yerevan", "Caucasus Standard Time"},
{"Zagreb", "Central European Standard Time"}
};
}
You can now use that with TimeZoneInfo
like so:
string windowsZoneId = TZConvert.RailsToWindows("Central Time (US & Canada)");
TimeZoneInfo central = TimeZoneInfo.FindSystemTimeZoneById(windowsZoneId);
Be aware of a few things:
Rails mappings seem to be relatively fixed, but CLDR mappings are occasionally updated. You may need to revisit this from time to time to make sure any new time zones are mapped appropriately.
The Rails time zone
"Chatham Is."
cannot currently be mapped to any Windows time zone. (There is no time zone defined for Chatham Islands in Windows.)If you wanted to be more accurate and support all zones, you could go from Rails to IANA/TZDB time zones instead of to Windows time zones. You could then use Noda Time with it's native TZDB support. This would also remove the need to use CLDR mappings.
if using docker use this
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine
RUN apk add --no-cache tzdata
reference: https://www.stevejgordon.co.uk/timezonenotfoundexception-in-alpine-based-docker-images
else go with this it takes iana_timezone
calling fn:
var timezone = "Asia/Kolkata";
var dtUtc = GetUtcByTimezone(timezone, utc);
public static DateTimeOffset GetUtcByTimezone(string timezone, DateTime? dateTime = null)
{
dateTime ??= DateTime.UtcNow;
var timeZoneInfo = TZConvert.GetTimeZoneInfo(timezone);
var dtUtc = TimeZoneInfo.ConvertTimeToUtc(dateTime.Value, timeZoneInfo);
return dtUtc;
}