Home > Net >  How to get browser timezone in javascript then convert into .net timezone id
How to get browser timezone in javascript then convert into .net timezone id

Time:08-28

I Just need to know the timezone from client browser then i need to convert some transactions into that client browser timezone. while R&D i understood one thing we cannot get browser timezone from c# so is it possible to get browser timezone using javascript then we can match that with .net timezone ?

CodePudding user response:

You are talking about converting IANA timezone to Windows.

IANA timezone: https://www.iana.org/time-zones

You can find an XML containing the IANA -> Windows mapping here

https://github.com/unicode-org/cldr/blob/main/common/supplemental/windowsZones.xml

You can deserialize that XML and look for the IANA timezone to get the Windows one:

<!-- (UTC 05:30) Chennai, Kolkata, Mumbai, New Delhi -->
<mapZone other="India Standard Time" territory="001" type="Asia/Calcutta"/>
<mapZone other="India Standard Time" territory="IN" type="Asia/Calcutta"/>

There's a NuGet package to convert between IANA and Windows timezone

https://www.nuget.org/packages/TimeZoneConverter

You cannot do this conversion on the client side but you can send the timezone name and then convert it on the server.

After installing the NuGet package you can convert a IANA timezone to Windows timezone ID with this code

string tz = TZConvert.IanaToWindows("America/New_York");
// Result:  "Eastern Standard Time"

BTW you should send data from the client (browser) in UTC and receive all data from the server in UTC and just convert to local before showing the data to the user. Also the data in database should be all in UTC.

  • Related