Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda