2
Answers

Fetching Date from Client Machine

Mukesh Sagar

Mukesh Sagar

11y
1.6k
1
I want to implement the date on the web application, where date is taken from the local machine; not from the server machine. Also i want to display the difference in time between the local machine and server machine also on the web page. Pl suggest some alternatives for the said problem.
Answers (2)
0
Mukesh Sagar

Mukesh Sagar

557 2.1k 27.3k 11y
The sample code to access the client machine date & time is as follows:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="timestamp">
        October 30, 2013 5:49 pm GMT        
    </div>
    <script type="text/javascript">
        var timestamp1 = document.getElementById('timestamp'),
        t = new Date(timestamp1.innerHTML),
        hours = t.getHours(),
        min = t.getMinutes() + '',s
        pm = false,
        months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

        if (hours > 11) {
            hours = hours - 12;
            pm = true;
        }

        if (hours == 0) hours = 12;
        if (min.length == 1) min = '0' + min;

        timestamp.innerHTML = months[t.getMonth()] + ' ' + t.getDate() + ', ' + t.getFullYear() + ' ' + hours + ':' + min + ' ' + (pm ? 'pm' : 'am');
    </script>
    </form>
</body>
</html>
0
Sanjay Kumar

Sanjay Kumar

NA 5k 4.4m 11y