javascript 显示当前系统时间的代码举例

发布时间:2020-06-26编辑:脚本学堂
本文介绍下,javascript实现显示当前系统时间的一例代码,供大家学习参考。

1,在<head></head>中加入:

<script language=javascript>
/**
* 显示当前系统时间
* edit www.jb200.com
*/var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;}
function startclock () {
stopclock();
showtime();}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" +((hours >= 12) ? "下午 " : "上午 " )
timeValue += ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
document.clock.thetime.value = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;}
</SCRIPT>

2,在<body>中加入:

onload=startclock()

3,在显示时间的位置插入以下代码:

<form name=clock >
<input name=thetime style="font-size: 9pt;color:#000000;border:0" size=12>
</form>
//只获得当前系统日期的函数
<script language="JavaScript" type="text/javascript">
document.write(new Date().getYear()+"-"+(new Date().getMonth()+1)+"-"+new Date().getDate());
</script>