Reputation: 388
I want Client timezone name (e.g. India Standard Time) in ASP.Net MVC Application.
So, I Can use TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName)
function.
I have tried with new Date().getTimezoneOffset()
(reference)
But, there is problem with Daylight Saving Time in this Code.
In javascript, i have also tried with new Date().toString()
But, this gives result in different format with different browser.
In firefox, chrome & safari, Answer is "Tue May 08 2012 14:00:00 GMT+0530 (India Standard Time)"
In IE8, it is "Tue May 8 14:00:00 UTC+0530 2012"
Similar problem with opera also.
So, this method will not work for IE & Opera.
What will be best way to identify Client TimeZone?
Upvotes: 2
Views: 8236
Reputation: 5203
Do you need to know the timezone or just the offset from UTC? Because timezones are not an ISO standard and can be changed almost arbitrarily, I prefer to work with the client's offset from UTC and I always get it from the client at login time or whenever they submit a form on which I need the client time. Sometimes I work out the GTM offset of the client and persist it to use wherever I display or use a time.
So I have a global JavaScript function like this which returns the given local date as a universal string:
function aspClientDateTime(dateObject) {
return dateObject.getFullYear() + '-'
+ this.leadingZero((dateObject.getMonth() + 1), 2) + '-'
+ this.leadingZero(dateObject.getDate(), 2) + ' '
+ this.leadingZero(dateObject.getHours(), 2) + ':'
+ this.leadingZero(dateObject.getMinutes(), 2) + ':'
+ this.leadingZero(dateObject.getSeconds(), 2);
}
On form submit, I get the current client date and store it in the form:
myForm.clientTime.val(aspClientDateTime(new Date()));
You then know what time it is in the browser and you can work out UTC offsets etc on the server.
The problem with timezones is that any government could introduce a new one tomorrow, as Samoa recently did. Typically I want to know what the offset of the user is right now, not what timezone they are in.
If it is the latter you really need, you might have to ask them, in the same way that Skype does it.
Upvotes: 3
Reputation: 8963
There is a library for detecting the timezone through JavaScript. It'll give you the name of a good enough timezone. It does not do geolocation or ip-lookup so it is very fast, but it is not extremely exact. For example it'll return "Europe/Berlin" for anyone in the Central European Timezone. It should be good enough for server side datetime normalizations however.
https://bitbucket.org/pellepim/jstimezonedetect
Upvotes: 0
Reputation: 12705
the best and the most utilized way is to identify clients location thru some 3rd party library and database utilizing the ip of the client.. and then based upon the location of the client decide the timezone
Upvotes: 3