简体   繁体   中英

How to get Client timeZoneOffset from server side code

How can I get getTimezoneOffset(); value from the client side in codebehind(aspx.cs file) on Page_load event?

I don't believe you can do this directly. I would have thought you could grab the Date from the Request.Headers and calculate this, but, at least in my environment, using my browser, the Date header is not accessible.

The only possible solution I can think of, since client info is typically limited to what's sent in the headers or in a form request would be to use Javascript to grab the headers, populate a form field (maybe a HiddenField) and trigger a postback.

I googled it and came up with this response, which shows pretty much how to do it the way I was thinking you'd need to - slightly differently than I would have done it, but close enough.

http://www.velocityreviews.com/forums/t70226-client-timezone.html

The code for the answer on that link is here:

<td>
<input type="button" value="getclientutc" onclick="GetClientUTC()">
<input type="hidden" id="hdClientUTC" runat="server">
</td>

:

function GetClientUTC()
{
var now = new Date()
var offset = now.getTimezoneOffset();
document.Form1.hdClientUTC.value = offset
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM