admin管理员组文章数量:1316855
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?
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?
Share Improve this question edited May 8, 2012 at 8:53 VisioN 145k34 gold badges287 silver badges289 bronze badges asked May 8, 2012 at 8:49 HarshHarsh 3883 silver badges20 bronze badges3 Answers
Reset to default 3the 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
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.
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/pellepim/jstimezonedetect
本文标签: javascriptHow to get Client TimeZone in ASPNet MVC ApplicationStack Overflow
版权声明:本文标题:javascript - How to get Client TimeZone in ASP.Net MVC Application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742010054a2412721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论