0

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 ?

Vasanth R
  • 172
  • 1
  • 1
  • 14
  • Does this answer your question? [Getting the client's time zone (and offset) in JavaScript](https://stackoverflow.com/questions/1091372/getting-the-clients-time-zone-and-offset-in-javascript) – Sund'er Aug 25 '22 at 05:40
  • this JavaScript code returning like "Asia/Calcutta" but we need something like "China Standard Time" , "Altai Standard Time" – Vasanth R Aug 25 '22 at 06:24
  • where are you running the project local or in the server? – Sund'er Aug 25 '22 at 06:42
  • A browser runs on the client machine and will report local time. The server is on a remote machine and only can get time if the client sends the time in a request. – jdweng Aug 25 '22 at 07:15

1 Answers1

1

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.

Paolo Iommarini
  • 219
  • 2
  • 9