Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the client timezone id for c# timezoneinfo class from client side using javascript

I want to get client timezone id from JavaScript to parse c# TimezoneInfo class.And Convert to utc time.And I have this

var timezone = String(new Date());
return timezone.substring(timezone.lastIndexOf('(') + 1).replace(')', '').trim();

Problem is some time it will javascript timezone return CST. Is there a proper way to get timezone id

and from the c#

TimeZoneInfo ZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneIdFromJavascript);
return TimeZoneInfo.ConvertTimeToUtc(Datetime, ZoneInfo);'
like image 951
Sanuja Lavindika Avatar asked Mar 03 '16 04:03

Sanuja Lavindika


1 Answers

  1. TimeZoneInfo uses Windows time zone identifiers. They are not going to match anything coming out of JavaScript. Please read the timezone tag wiki.
  2. Identifying the user's time zone in JavaScript is imperfect. You can guess at it, using one of these methods, but that is going to give you an IANA time zone id, not a Windows time zone id.
  3. You can convert IANA time zones to Windows time zones using either my TimeZoneConverter library, or the method described here which uses Noda Time. However if you're going to use Noda Time, you might as well just use IANA time zones in the first place. Noda Time does a much better job than TimeZoneInfo.
  4. If you just want to convert the client's local time to UTC, then just do that in the browser in JavaScript. Then you don't need to know what the local time zone actually is.
like image 63
Matt Johnson-Pint Avatar answered Sep 16 '22 22:09

Matt Johnson-Pint