May 20, 2014 by Nitesh

How To Retrieve Client’s Date in ASP.Net – Easiest way

Friends,

A very short post for a simple task. In this post, we will see how we can get the date of a user browsing the page on server in ASP.Net. DateTime.Now gives us the server Date & Time. Retrieving Client’s date is required in cases when you want to process something based on browser’s date and not Server’s date. To do this, we will use a hidden field and a bit of jQuery.

  • In the ASPX page, declare a hidden field. lets name it as hdnClientDate.
         <asp:hiddenfield ID="hdnClientDate" runat="server"></asp:hiddenfield>
    
  • Add the below jQuery that will auto populate the hidden field with the current date when the page is loaded.
         <script type="text/javascript">
            $(document).ready(function () {
                $("#< %=hdnClientDate.ClientID%>").val(new Date().toJSON().slice(0, 10));
            });
        </script>
    
  • Parse the date in server side on any Postback.
         DateTime dd;
         DateTime.TryParse(hdnClientDate.Value, out dd);
    

Keep Learning and sharing! Cheers!

#ASP.Net#Date#jQuery#Utilities

Support us!

If you like this site please help and make click on any of these buttons!

Powered by WordPress Popup