Detect Timezone of client using c#
It is not possible to do this in a reliable way purely with C#. The HTTP spec does not provide any hints of the client's time zone.
You can guess at a client's time zone by using IP geolocation, and there are many free and commercial services that provide time zone information. However there is only a limited degree of accuracy with such an approach.
Even with JavaScript, the current solutions can still only guess the client's time zone. There are improvements happening for this with in the ECMAScript Intl API, but not all browsers fully implement that yet. See more here.
Ultimately, if you need to know the time zone of the client from your back end code, the best thing to do is to ask the user. Provide a time zone picker somewhere in your application.
You may also find this related answer useful.
Angular 5 and .NET Core 2.1 API: Getting current time in user's timezone
Make shure that you send every Timestamp as UTC to the client. Something like "2018-12-12T20:13:00.000Z". If you convert that to a JavaScript Date it should display automatically in the local time zone of the Browser.
Get Local TimeZoneInfo
yes you are right, this is what you should use. Alternative can be
TimeZone localZone = TimeZone.CurrentTimeZone;
but the CurrentTimeZone
property corresponds to the TimeZoneInfo.Local
property so no difference really.
it displays the names for standard time and daylight saving time for the local time zone.
Finding timezone in HttpRequest from server side
The date header is not one that is sent in standard http requests. I just ran a quick check with fiddler using both IE and Firefox and didn't see the date header sent on any requests.
The best that you can do on the server is get the user's culture, but this will only help with the date format, not the timezone.
However, you can get the information from javascript using getTimezoneOffset. For example:
var timeNow = new Date();
var timezone = timeNow.getTimezoneOffset() / 60 * (-1);
There is an excellent description here.
How do I get the current date in a user's timezone, given an TimeSpan offset?
string timezone = this.HttpContext.Request.Headers["X-MyApp-Timezone"];
DateTime consumersCurrentDateTime = DateTime.UtcNow + TimeSpan.Parse(timezone,
DateTimeFormatInfo.InvariantInfo);
DateTime customersCurrentDate = consumersCurrentDateTime.Date;
Related Topics
Check If a Value Is in an Array (C#)
Setting Datacontext in Xaml in Wpf
How to Connect to an Mdf Database File
How Big Is an Object Reference in .Net
Deserializing JSON to Abstract Class
Why Is the 'This' Keyword Required to Call an Extension Method from Within the Extended Class
Fastest Method to Remove Empty Rows and Columns from Excel Files Using Interop
Serialize Dictionary as Array (Of Key Value Pairs)
C# - Get a List of Files Excluding Those That Are Hidden
How to Retrieve Data Annotations from Code? (Programmatically)
How to Call a Vbscript File in a C# Application
Getting the Absolute Path of the Executable, Using C#
How to Get the Index of an Element in an Ienumerable
List<T> Orderby Alphabetical Order
Mail Sending with Network Credential as True in Windows Form Not Working