tags:

views:

36

answers:

2

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

A: 

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>
David Stratton
Thank you David for the link.. the #6 post in that link specifies how to get the value in page_load.. but I am not able to implement that in my code.. I am posting the code below
helpplease
A: 

`

 <form id="Form1"  style="margin-left:25px; margin-top:25px;">
   <input type="hidden" name="hidClientUTCOffset" id="hidClientUTCOffset" runat="server" />      
</form

`

I tried changing the form to runat="server" also but didn't got the value back in Page_Load

`

String strScript = "var now = new Date();  ";
 strScript += "var offset = now.getTimezoneOffset();  ";
 strScript += "document.getElementById('hidClientUTCOffset').value = offset;document.Form1.submit(); ";
 ScriptManager.RegisterStartupScript(this, typeof(string), "GET_OFFSET", strScript, true);
 if (hidClientUTCOffset.Value != "")
 {
     string strOffset = hidClientUTCOffset.Value;
 }

`

helpplease