Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Retrieve Timezone as variable from javascript to asp.net codebehind

Although this question may seem as a duplicate of other questions, it is not.

I have a basic asp.net web application and I simply want to retrieve client time. Here are the problems I encountered:

1- Using C# code DateTime.Now basically gives the dateTime of the place of the server. So obviously it is not useful.

2- Then, I tried to get the datetime from JavaScript (browser time). Past stackoverflow questions suggest that I should use a HiddenField. But it is useful as long as a button ignites a functions which would assign datetime value to a hiddenfield. I tried to assign the datetime value to hiddenfield with window.onload function (so that hiddenvalue can get datetime when the page loads). Another problem occured: according to asp.net page lifecycle, javascript code comes after the page_load event. So another fail.

3- Here is the ultimate solution I came up with:

First, I wrote a JS function that creates a cookie of the offset when called:

function createZoneCookie() {

            var zone = new Date().getTimezoneOffset() / -60

            var date = new Date();
            date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();

            document.cookie = "clienttzone=" + zone + expires + "; path=/";
        }

Then, I used the following code located in the codebehind(c#):

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["clienttzone"]==null)
        {
            ScriptManager.RegisterStartupScript(this,GetType(), "Javascript", "javascript:createZoneCookie(); ", true);

        }
    }

It works fine, but!! As you know user can access to my js function using the inspect (F12) button in their browser. Thus, they can alter my code and give a false dateTime or even worse create their own arbitrary cookies. And as I was told, it is impossible to make a JS code invisible to the user.

How the hell do people retrieve user timezone without compromising sensitive code?

PLUS Is there a way by which the server side code can use a client-side returning-function? Let me clarify:

ScriptManager.RegisterStartupScript(this,GetType(), "Javascript", "javascript:createZoneCookie(); ", true);

I have used the above code to run a function from the server side but as you know the createZoneCookie function is a void-type function.

My question is: can I get a value from a JS function from the C# side of my code?

this question didn't work because the accepted answer doesn't adress to the question of serverside-clientside value pass.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!