0

A user(mobile app) sends me the current login time and I am converting it to the specific time zone, like in indian time 12:55:34 GMT+0530 (India Standard Time). When I am passing India Standard Time to TimeZoneInfo it executes perfectly, but when I am passing only IST it throws this error:

The time zone id was not found on the local computer.

So how do I convert this IST to Indian standard time :( because some users send me zone abbreviation ie. 12:55:34 GMT+0530 (IST) :(

DateTime targetTime = TimeZoneInfo.ConvertTimeToUtc(datetime, userzone);
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Show the code where you create/assign the datetime and userzone parameters. – Polyfun Dec 18 '19 at 09:33
  • 6
    You can't. There's no such standard abbreviation. It's just an informal acronym. Take BST for example - British Summer Time or Bangalore Standard Time? Even `India Standard Time` isn't standard. The only standard names are the [IANA timezone names](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). `TimeZoneInfo ` works with the *Windows timezone names*, not the ambiguous abbreviations – Panagiotis Kanavos Dec 18 '19 at 09:34
  • @PanagiotisKanavos - yes i am agree with you but how to handle this situation :( –  Dec 18 '19 at 09:42
  • i want to convert each user time to 6 AM as per his time zone then this convert to UTC –  Dec 18 '19 at 09:43
  • 2
    You use `India Standard Time` or create a mapping in code, a database or settings file that uses the mappings *you* want. – Panagiotis Kanavos Dec 18 '19 at 09:51
  • @PanagiotisKanavos simply i want current user 6:00 Am UTC time as per his timezone i.e for Indian standard time user morning 6:Am UTC time is 00:30 like that for all user as per his time zone i want 6:00 AM UTC time –  Dec 18 '19 at 09:57
  • 1
    Use [TimeZoneInfo.FindSystemTimeZoneById](https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.findsystemtimezonebyid?view=netframework-4.8) with `India Standard Time` as a parameter. You'll have to map `IST` to `India Standard Time` yourself. – Panagiotis Kanavos Dec 18 '19 at 10:03
  • 2
    Wikipedia [suggests](https://en.wikipedia.org/wiki/Ist#Time_zones) `IST` may be one of: `Indian Standard Time`, or `Irish Standard Time`, or `Israel Standard Time`. There may be more, of course. Confusingly, "Irish Standard Time" seems to be the __summer time__ of that republic (i.e. daylight savings time). – Jeppe Stig Nielsen Dec 18 '19 at 10:27
  • You could use the `TimeZoneNames` package to get abbreviations per country or timezone, [as shown in this answer](https://stackoverflow.com/a/15304384/134204). If the *real* question is how to parse non-standard time strings that include the abbreviation or timezone name, things are more difficult. – Panagiotis Kanavos Dec 18 '19 at 10:29
  • @PanagiotisKanavos : but i want full name :( from abbreviation –  Dec 18 '19 at 10:34
  • ok is their any third party library which take abbreviation and timezone like ("IST","GMT+0530") that what i found unique in Wikipedia –  Dec 18 '19 at 10:48
  • 2
    The [the Wikipedia timezone abbreviations list](https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations) shows that `IST` maps to `Irish Standard Time`, `Israel Standard Time` or `India Standard Time`. It's definitely *not* unique. You can use that list as a source and try to find the name using the abbreviation and offset to pick one of the three timezone names whose abbreviation is IST. Or you could search in NuGet for any packages that already do this. The code isn't hard though. You can just copy that table and paste it eg into a JSON file, replace tabs with `","` etc – Panagiotis Kanavos Dec 18 '19 at 10:52
  • 1
    Please clarify *exactly* what the string looks like that you receive? If it contains the date, time, and offset, then you don't actually need to parse the time zone abbreviation at all. Also, are you in control over this application? Can you change what it sends? – Matt Johnson-Pint Dec 18 '19 at 18:20

0 Answers0