How to get client current date and time in asp.net?

前端 未结 6 1805
梦如初夏
梦如初夏 2021-01-07 02:52

I have a hidden field in my master page. And I set the current date and time as a value in that \"hiddenfield\" using Javascript. I am unable to get this hidden field value

6条回答
  •  孤街浪徒
    2021-01-07 03:17

    I have done this by using following way:

    1. I have taken a HiddenField into the previous page where it requires :

      
      
    2. Then, I am getting browser timezone by this :

      $(document).ready(function () {
           var offset = new Date().getTimezoneOffset();
           $('#hdnTimeOffset').val(offset);
      
       });
      
    3. I am getting time zone into a Session variable of the previous page, before redirecting to the next page, like on button click:

      protected void btn_Click(object sender, EventArgs e)
      {
          Session["TimeOffset"] = hdnTimeOffset.Value;
          Response.Redirect("~/Root/abc.aspx");
      }
      
    4. In the Main Page, I get the current time by using following code :

      DateTime dtmCurrentDateTime = DateTime.Now.ToUniversalTime();
      double dblMinutes =Convert.ToDouble(Session["TimeOffset"].ToString());                
      dtmCurrentDateTime = dtmCurrentDateTime.AddMinutes(-dblMinutes);
      

提交回复
热议问题