How to get client(PC) date and time in asp.net?

{0 Comments}

Getting time and date of user’s personal computer is little bit tricky in asp.net because using most popular code which is below we can get only the server date and time which is not the exact solution when you need the date time of your personal or any ones computer. 

 C# |  copy code |? 
1
DateTime.Now.ToString();

Solution for this problem can be done by using small JavaScript tricky function which is as below and using this you can get the PC’s time not of the server. You can see the live example by clicking on the this link which will give the server as well as pc’s time to test just change the time of your PC and reload the same link 

 Javascript |  copy code |? 
01
<script type="text/javascript">
02
    function displayTime()
03
    {
04
        var localTime = new Date();
05
        var year= localTime.getYear();
06
        var month= localTime.getMonth() +1;
07
        var date = localTime.getDate();
08
        var hours = localTime .getHours();
09
        var minutes = localTime .getMinutes();
10
        var seconds = localTime .getSeconds();
11
        var div=document.getElementById("div1");
12
        div.innerText="your PC's time"+year+"/"+month+"/"+date+" "+hours+":"+minutes+":"+seconds;
13
    }
14
    </script>

Leave a Comment

Your email address will not be published.