welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, April 15, 2013

How to get the client side user time in ASP.Net

Here I would like to share easy code to get client side time. In many case we need to show the client side time.
Take scenario you host your website on UK server, but the user accessing your site from India, and he try post some content on your website. Here you have to show the client side posting time. If you get the time from server it will show UK time, which means its wrong time because of time difference between two countries.
So to overcome above problem you can easily solved by using JavaScript.

<script type="text/javascript">
        function getTime()
            TimeFd = document.getElementById("ClientTime");
            clientDate = new Date();
            // return hours and minutes so we don't have to
            // worry about 12/24 hour settings.
            TimeFd.value = clientDate.getHours() + ":" + clientDate.getMinutes();

On body tag you should call the JavaScript function on which page loads its will call the function to display client side time.
<body onload="getTime();">
  <asp:HiddenField ID="ClientTime" runat="server" />

That’s it .By using the above method you can easily store the client side time value.
Post your Comments Here


Post a Comment