2

I want to display the system time on my jsp page. How can i do it? I'm trying this but only date is getting displayed that and not the time. It's all working fine in Internet Explorer but not in other browsers.

  <td colspan="1" height="4" align="left" width="260" >
       <font class="welcome1">
            <strong>
                <script language="JavaScript" src="js/date.js"></script>
                <span id="clock">
                    <script language="JavaScript" 
                                            src="js/digitalClock.js"></script>
                </span>
            </strong>
       </font>
 </td>
kravemir
  • 10,636
  • 17
  • 64
  • 111
Bangaram
  • 51
  • 1
  • 1
  • 8
  • 2
    How can we possibly help you without seeing the js files? – PeeHaa Jul 22 '11 at 08:24
  • Why are you using JavaScript for this, when you've got a server-side language (JSP, according to your question) to work with? Putting the date into the markup would be much more appropriate, and would support progressive enhancement / graceful degradation. – Joe White Jul 22 '11 at 16:17
  • Because he wants to have a clock that runs like a real clock, and not only when f5-pressing I suppose. So Js is perfect. – Timo Feb 13 '14 at 20:35

6 Answers6

11

Displaying the time on a web-page using js should be trivial .

 new Date().toLocaleString() // displays date and time
 new Date().toLocaleDateString() // displays date
 new Date().toLocaleTimeString() // displays time
amal
  • 1,369
  • 8
  • 15
6

To display time you can use Date.

<html>
<head>
<script type="text/javascript">
    <!--
    function updateTime() {
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        if (seconds < 10){
            seconds = "0" + seconds;
        }
        var v = hours + ":" + minutes + ":" + seconds + " ";
        if(hours > 11){
            v+="PM";
        } else {
            v+="AM"
        }
        setTimeout("updateTime()",1000);
        document.getElementById('time').innerHTML=v;
    }
    updateTime();
    //-->
</script>
</head>
<body>
    <h4>Current Time: <span id="time" /></h4>
</body>
</html>

I've tested it in firefox and chrome. Found on this site.

Edit: time now gets updated every second.

kravemir
  • 10,636
  • 17
  • 64
  • 111
  • Thank you!!! But the time should change every second :-( I mean it should get updated every second.... – Bangaram Jul 22 '11 at 10:30
2

You should read the documentation about the Date object in JavaScript (Date). It would be easier if you post the JS source.

Riskz
  • 239
  • 1
  • 11
1

/* set Date */
function tick() {

  /* Get date in epoch */
  var epoch = Date.now();    
  document.querySelector("#epoch").innerHTML = epoch;
  
  /* Separate epoch */
  var datetime = new Date(epoch);

  var year    = datetime.getFullYear();
  var month   = datetime.getMonth() + 1; // (0-11)
  var date    = datetime.getDate();
  var hour    = datetime.getHours();
  var minute  = datetime.getMinutes();
  var second  = datetime.getSeconds();

  document.querySelector("#datetime").innerHTML =
    year + "-" + addZero(month) + "-" + addZero(date) + " " + 
    addZero(hour) + ":" + addZero(minute) + ":" + addZero(second);      

}

// Add 0 if argument < 10
function addZero(i) {
  if (i < 10) {
    i = "0" + i
  };  
  return i;
}

/* Call tick in interval 1 second */
setInterval(tick, 1000);
<p id="epoch"></p>
<p id="datetime"></p>
antelove
  • 3,216
  • 26
  • 20
0

This seems the easiest solution, which uses setInterval with two arguments, the first being a callback, the second the interval in ms:

setInterval(function(){ document.write(new Date().toLocaleTimeString();

},1000);

So my solution should be the accepted answer because it is a real time.

Community
  • 1
  • 1
Timo
  • 2,922
  • 3
  • 29
  • 28
0

For Date You can use below Javascript. Time will display in a text box. You can modify as per your requirement.

<html>
<head>
<script type="text/javascript">
var timer = null
function stop()
{
clearTimeout(timer)
}
function start()
{
var time = new Date()
var hours = time.getHours()
var minutes = time.getMinutes()
var seconds = time.getSeconds()
var clock = hours + ":" + minutes + ":" + seconds
document.forms[0].display.value = clock
timer = setTimeout("start()",1000)
}
</script>
</head>
<body onload="start()" onunload="stop()">
<form>
<input type="text" name="display" size="20">
</form>
</body>
</html>