Reputation: 13582
I have a common problem:
Need to convert time between Time Zones
1- I need to find what is the Client Side Time Zone
2- Convert it To UTC
So after some search I find that the only way to find the client side Time Zone is Use java script, and I also Use This Library, But this library returned the Time zone with this format: Location/CityName
Like : "America/New_York"
Then the only way I know to convert Time between Time Zones is:
TimeZoneInfo.ConvertTimeToUtc(clientsidedatetime, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time") /*Client Time Zone Name*/);
So as you see the Time Zone Format used in FindSystemTimeZoneById
is different from my java script returned.
The question is how can I convert this two format to each other, for ex Convert Asia/Singapore
to Singapore Time
?
Or is there any better way to find and convert client side time to UTC Time?
Upvotes: 3
Views: 2365
Reputation: 241475
Timezones like "America/New York" are the standard on unix/linux based systems and on the internet in general. They are called "Olson" timezones, after the original author, Arthur Olson. The database they are based on is sometimes called the "Olson database", "tz database" or "zoneinfo database". They are now maintained by IANA. You can read more about them on Wikipedia.
Olson timezones are not at all compatible with Microsoft Windows timezone identifiers - which the .Net TimeZoneInfo classes use. Microsoft has long had their own timezone database, which is maintained by Windows Update, and discussed by Microsoft here.
The authoritative source for converting between these standards is the Unicode CLDR They publish a table that can be used for conversion, but I recommend you use a library for this instead.
There are several libraries available for .Net for working with Olson timezone and CLDR data. The best (IMHO) is NodaTime by StackOverflow celeb Jon Skeet. This is a port of a highly popular library from Java that includes an Olson timezone implementation, along with a reworked set of classes for datetime manipulation that are superior to Microsoft's in many ways.
The libraries I am aware of that implement the IANA/Olson database in .Net are:
UPDATE
All of this information, and more, are available in the StackOverflow TimeZone tag wiki.
Upvotes: 3