本文實例講述了js實現Form欄顯示全格式時間時鐘效果代碼。分享給大家供大家參考。具體如下:
這裡演示的Form欄特效時鐘,顯示全日期格式的時間日期效果,可顯示星期幾、幾月幾號,以往都是直接顯示在網頁中,而本代碼是將時間顯示在表單的文本框中,看一下代碼就明白了。
運行效果截圖如下:
在線演示地址如下:
http://demo.jb51.net/js/2015/js-form-input-showtime-codes/
具體代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Form欄特全時鐘</TITLE> </HEAD> <body bgcolor="#ffffff" onLoad="startclock()"> <script language="JavaScript"> <!-- Hide var timerID = null var timerRunning = false function MakeArray(size) { this.length = size; for(var i = 1; i <= size; i++) { this[i] = ""; } return this; } function stopclock (){ if(timerRunning) clearTimeout(timerID); timerRunning = false } function showtime () { var now = new Date(); var year = now.getFullYear(); var month = now.getMonth() + 1; var date = now.getDate(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds(); var day = now.getDay(); Day = new MakeArray(7); Day[0]="星期天"; Day[1]="星期一"; Day[2]="星期二"; Day[3]="星期三"; Day[4]="星期四"; Day[5]="星期五"; Day[6]="星期六"; var timeValue = ""; timeValue += year + "年"; timeValue += ((month < 10) ? "0" : "") + month + "月"; timeValue += date + "日 "; timeValue += (Day[day]) + " "; timeValue += ((hours <= 12) ? hours : hours - 12); timeValue += ((minutes < 10) ? ":0" : ":") + minutes; timeValue += ((seconds < 10) ? ":0" : ":") + seconds; timeValue += (hours < 12) ? "上午" : "下午"; document.jsfrm.face.value = timeValue; timerID = setTimeout("showtime()",1000); timerRunning = true } function startclock () { stopclock(); showtime() } //--> </script> <form name='jsfrm'> <input type=text name='face' size=34 value=''> </form> </BODY> </HTML>
希望本文所述對大家的javascript程序設計有所幫助。