How to get local user time in Page_Load

I have a web page written in ASP.NET and I need to get the local end-user time in Page_Load . I thought about using Javascript to get local time (using new Date() ), but the problem is that the script is being run after server events.

Any ideas on how to do this?

EDIT: my page is rather complicated: it displays a diagram with a large number of calculated fields from the database, object / field select lists, etc .; The client now asked that it take into account the user's time zone and that the time zone should be determined automatically by the web page. The user date is important for determining the interval of the chart (that day to display data). Data loading (since it is so complex) is performed both in Page_Load and in Page_PreRender . Failure to do so will require a complete rewrite of the page.

FINAL DECISION WITH INSPIRED ANSWER: This is how I solved the problem in the end. I save the local date in a cookie. Here is the method that sets the cookie:

 function SetLocalDateCookie() { var cookieName = 'LOCALDATE'; var localDate = new Date(); var realMonth = localDate.getMonth() + 1; var localDateString = localDate.getFullYear() + "/" + realMonth + "/" + localDate.getDate(); setCookie(cookieName, localDateString, 2); try { var exdate = new Date(); exdate.setDate(exdate.getDate() + 2); document.cookie = cookieName + "=" + escape(localDateString) + ";expires=" + exdate.toGMTString(); } catch (e) { } } 

On my main page, I call this method on $(document).ready . On the page where I use this cookie, I added the following Page_Init code:

 if (string.IsNullOrEmpty(CookieHandler.Instance.GetCookie(CookieKeys.LocalDate))) { Response.ClearContent(); Response.Write(@"<form id='local' method='post' name='local'> <script type='text/javascript'> SetLocalDateCookie(); document.getElementById('local').submit(); </script> </form>"); Response.Flush(); Response.End(); } 

Then I can just use the cookie value in C # code. Thanks for your answers / comments!

+7
source share
5 answers

I will explain the following code a bit and what you need to do.

At the first request from this page, the code checks if LocalTime has already been saved in the session, and if it will not write the form element, hidden input and javascript that will publish this form with local time. The response ends, so your report will not get the opportunity to be generated.

This submit will immediately create a POST request with localTime set, then ASP.Net will save that POST value in the session.

I also added a 302 redirect (Response.Redirect) to the original page due to usability. The user first made a GET request, not a POST, so if he wants to refresh the page, the browser will repeat the last action, which was that form.submit (), not a GET request.

You have local time. But you do not need to read it for each request, since it can be compared with UTC time, and then with a time server.

edit: you need to parse the UTC time in DateTime, but it is probably easy to find the format, although it may depend on the culture of the user (not sure about this statement).

 public ReportPage() { this.Init += (o, e) => { // if the local time is not saved yet in Session and the request has not posted the localTime if (Session["localTime"] == null && String.IsNullOrEmpty(Request.Params["localTime"])) { // then clear the content and write some html, a javascript code which submits the local time Response.ClearContent(); Response.Write(@"<form id='local' method='post' name='local'> <input type='hidden' id='localTime' name='localTime' /> <script type='text/javascript'> document.getElementById('localTime').value = new Date(); document.getElementById('local').submit(); </script> </form>"); // Response.Flush(); // end the response so PageLoad, PagePreRender etc won't be executed Response.End(); } else { // if the request contains the localtime, then save it in Session if (Request.Params["localTime"] != null) { Session["localTime"] = Request.Params["localTime"]; // and redirect back to the original url Response.Redirect(Request.RawUrl); } } }; } 
+4
source

I do not think that it would be possible, you cannot lose time on the client's local machine on the server side.

The only way to achieve this is to use javascript (since this is the client interface, so it will use the current date / time of the client). But, as you said, this will happen after your server events are fired and your web page is displayed in HTML and sent to the client.

One option would be to capture client time before Post Back, but it would be impossible to do this using inital Page_Load .

0
source

How about you place a hidden text field on a page, attach an OnChange event handler to it in C # and use the JavaScript OnLoad function to set the value for the value you need from the client?

0
source

As far as I know, there is no way to get the user's local time in the Page_Load event, which is executed on the server side, and therefore will only know about the local server date time.

To get the user's local time, you need to either make an asynchronous call on the server when loading the page in JavaScript, or save the local time in a hidden field, and then read it on the back.

0
source

Or just use an interstitial page that contains nothing but js to get local time and redirect time information to your current page via a query string or session variable or message

0
source

All Articles