Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:35:38+00:00 2026-06-01T02:35:38+00:00

I have a web page written in ASP.NET and I need to retrieve the

  • 0

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

Any ideas on how to accomplish this?

EDIT: My page is quite complex: it displays a chart with lots of calculated fields from a database, object/fields selection lists, etc; The customer now requested that it should consider the user’s timezone and that the timezone should be determined automatically by the web page. The user date is important to determine the chart interval (which day to display data on).
Data loading (since it is so complicated) is done in both Page_Load and Page_PreRender. Giving up these events would require a full page rewrite.

FINAL SOLUTION INSPIRED BY ANSWER:
Here is how I solved the problem eventually. I am keeping 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)
    { }
}

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

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 the C# code.
Thank you for your answers/comments!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:35:39+00:00Added an answer on June 1, 2026 at 2:35 am

    I’ll explain a bit the following code and what lefts for you to do.

    At the first request off this page, the code checks if the LocalTime is not already stored in Session and if not it will write a form element, a hidden input and a javascript which will post that form with the local time. The response ends, so your report won’t get the chance to be generated.

    This submit will immediately create a POST request with the localTime set, then ASP .Net stores this POST value into the Session.

    I also added a 302 redirect (Response.Redirect) to the original page, because of the usability. The User made initially a GET request, not a POST, so if he/she wants to refresh the page, the browser will reiterate the last action, which was that form.submit() and not the GET request.

    You have now the local time. But you don’t have to read it at every request since it can be compared to the UTC time, then with the server’s time.

    edit: You need to parse the UTC time into a DateTime, but probably it’s easy to find the format, though might depend on the user’s culture (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);
                }
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an asp.net web page written in C#. Using some javascript I popup
I have written an ASP.NET web page with C# behind that runs an existing
I have a web page which was written on ASP.NET and C#. it outputs
I have a web application written in ASP.NET, Framework 4.0. The web app is
I have a web-application written on ASP.NET MVC 3. On client side I used
I have a ASP .Net web appliaction written in Visual Basic .Net running on
We have a web application written in ASP.NET for .NET 3.5, using standard web
I have a system written in ASP.Net 2.0 Web Form. The framework that talks
In my asp.net web application I need to capture the Client time and store
I have a web site written using ASP.NET MVC3. The site has one single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.