tags:

views:

908

answers:

2

How do you find out the local time of the user browsing your website in ASP.NET?

A: 

You have to use JavaScript in the client side that will get the local time and pass that value to server side. You can pass it using querystring, hidden fields or even AJAX magic.

Albert
+3  A: 

Create a hidden input control to get the value back at the server on the next page postback. It will need to be populated it in the onload event for the page with the value of a new Date object created in JavaScript. You will want to create a JavaScript function to do this and add that to the page using RegisterClientScriptBlock.

Something like this;

function setNow(hiddenInputId)
{
     var now = new Date();
     var input = document.getElementById(hiddenInputId);

     if(input) input.value = now.toString();
}

Then create a call to the function passing the ClientId of the hidden input and then add that piece of script to the page using RegisterStartupScript so that it runs on page load.

Dave Anderson