如何在网页中动态显示当前日期和时间

JavaScript08

如何在网页中动态显示当前日期和时间,第1张

网页中动态显示当前日期和时间是使用了js程序,方法如下:

将一下程序插入页面

<script language=Javascript>

  function time(){

    //获得显示时间的div

    t_div = document.getElementById('showtime')

   var now=new Date()

    //替换div内容

   t_div.innerHTML = "现在是"+now.getFullYear()

    +"年"+(now.getMonth()+1)+"月"+now.getDate()

    +"日"+now.getHours()+"时"+now.getMinutes()

    +"分"+now.getSeconds()+"秒"

    //等待一秒钟后调用time方法,由于settimeout在time方法内,所以可以无限调用

   setTimeout(time,1000)

  }

</script>

修改<body>为<body  onload="time()">以便js程序加载运行

插入<div id="showtime"></div>用以显示日期时间的容器。

<html>

<head>

<title>setTimeout()</title>

<style type="text/css">

input

{

font-size:30px

border-style:none

background-color:#ff8b3b

}

</style>

</head>

<body onLoad="disptime()">

<form name="myform" >

<table width="100%" border="0" align=center>

<tr>

<td width="37%"> </td>

<td width="41%"><h2>当前时间:<input name="myclock" type="text" value=" " size="10"></h2></td>

<td width="22%"> </td>

</tr>

</table>

</form>

</body>

</html>

<script type="text/javascript">

function disptime()

{

var time=new Date()

var hour=time.getHours()

var minute=time.getMinutes()

var second=time.getSeconds()

document.myform.myclock.value=hour+":"+minute+":"+second+" "

var myTime=setTimeout("disptime()",1000)

}

</script>

我刚刚写的