一、实时走动的数字时钟 如:下午14:15:00
第一步:在<body></body>区域加入以下代码
XML/HTML代码 <script> function tick() { var hours, minutes, seconds, xfile; var intHours, intMinutes, intSeconds; var today; today = new Date(); intHours = today.getHours(); intMinutes = today.getMinutes(); intSeconds = today.getSeconds(); if (intHours == 0) { hours = 12:; xfile = 午夜; } else if (intHours < 12) { hours = intHours+:; xfile = 上午; } else if (intHours == 12) { hours = 12:; xfile = 正午; } else { intHoursintHours = intHours - 12 hours = intHours + :; xfile = 下午; } if (intMinutes < 10) { minutes = 0+intMinutes+:; } else { minutes = intMinutes+:; } if (intSeconds < 10) { seconds = 0+intSeconds+ ; } else { seconds = intSeconds+ ; } timeString = xfile+hours+minutes+seconds; Clock.innerHTML = timeString; window.setTimeout(tick();, 100); } window.onload = tick; </script>
第二步.将下面的代码加入html文件任意需要的地方
<div id=Clock align=center style=font-size: 20; color:#000000></div>
你可以自行更改样式!
二、显示年月日格式的时间代码 如:2008年11月27日 星期四
XML/HTML代码 <script language=javascript> today=new Date(); function initArray(){ this.length=initArray.arguments.length for(var i=0;i<this.length;i++) this[i+1]=initArray.arguments[i] } var d=new initArray( 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六); document.write( <font color=##000000 style='font-size:9pt;font-family: 宋体'> , today.getYear(),年, today.getMonth()+1,月, today.getDate(),日, d[today.getDay()+1], </font> ); </script>
三、显示日期,星期,时间格式的代码 如:2008年11月27日星期四 下午2:17:36
XML/HTML代码 <SCRIPT language=javascript> today=new Date(); function initArray(){ this.length=initArray.arguments.length for(var i=0;i<this.length;i++) this[i+1]=initArray.arguments[i] } var d=new initArray( 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六); document.write( <font color=#ecc1c1 style='font-size:9pt;font-family: 宋体'> , today.getYear(),年, today.getMonth()+1,月, today.getDate(),日, d[today.getDay()+1], </font> ); </SCRIPT> </TD> <TD class=bai align=left width=95> <SCRIPT language=javascript> function tick() { var hours, minutes, seconds, xfile; var intHours, intMinutes, intSeconds; var today; today = new Date(); intHours = today.getHours(); intMinutes = today.getMinutes(); intSeconds = today.getSeconds(); if (intHours == 0) { hours = 12:; xfile = 午夜; } else if (intHours < 12) { hours = intHours+:; xfile = 上午; } else if (intHours == 12) { hours = 12:; xfile = 正午; } else { intHours = intHours - 12 hours = intHours + :; xfile = 下午; } if (intMinutes < 10) { minutes = 0+intMinutes+:; } else { minutes = intMinutes+:; } if (intSeconds < 10) { seconds = 0+intSeconds+ ; } else { seconds = intSeconds+ ; } timeString = xfile+hours+minutes+seconds; Clock.innerHTML = timeString; window.setTimeout(tick();, 100); } window.onload = tick; </SCRIPT> <DIV id=Clock style=FONT-SIZE: 12px; COLOR: #ecc1c1 align=center></DIV>
四、显示来访者的停留时间 如:停留时间: 00:15
XML/HTML代码 <script language=javascript> var ap_name = navigator.appName; var ap_vinfo = navigator.appVersion; var ap_ver = parseFloat(ap_vinfo.substring(0,ap_vinfo.indexOf('('))); var time_start = new Date(); var clock_start = time_start.getTime(); var dl_ok=false; function init () { if(ap_name==Netscape && ap_ver>=3.0) dl_ok=true; return true; } function get_time_spent () { var time_now = new Date(); return((time_now.getTime() - clock_start)/1000); } function show_secs () // show the time user spent on the side { var i_total_secs = Math.round(get_time_spent()); var i_secs_spent = i_total_secs % 60; var i_mins_spent = Math.round((i_total_secs-30)/60); var s_secs_spent = + ((i_secs_spent>9) ? i_secs_spent : 0 + i_secs_spent); var s_mins_spent = + ((i_mins_spent>9) ? i_mins_spent : 0 + i_mins_spent); document.fm0.time_spent.value = s_mins_spent + : + s_secs_spent; window.setTimeout('show_secs()',1000); } // --> </script> <FORM name=fm0 onSubmit=0><FONT COLOR=#6060FF>停留时间: </FONT><INPUT type=text name=time_spent size=7 onFocus=this.blur()></FORM> 而且还要在你主页源文件<body>中加入下面的代码 <BODY onLoad=init(); window.setTimeout('show_secs()',1);>
五、显示当前日期与时间 如:现在是: 2008年11月27日 14:18:59 时间不走动
XML/HTML代码 <script language=javascript> <!-- now = new Date() hour = now.getHours() if (hour < 12) { document.write(现在是: + now.toLocaleString()) } else if (hour < 18){ document.write(现在是: + now.toLocaleString()) } else if (hour >= 18) { document.write(现在是: + now.toLocaleString()) } // --></script>
六、浏览器状态栏显示的时钟 如:2:20 P.M. 星期四 在状态栏显示
XML/HTML代码 <BODY onLoad=run(); timerONE=window.setTimeout></BODY> <SCRIPT LANGUAGE=javascript> <!-- Hide timeID = null; timeRunning = false; function stop () { if(timeRunning) clearTimeout(timeID); timeRunning = false; } function time () { tick = new Date(); hours = tick.getHours(); minutes = tick.getMinutes(); seconds = tick.getSeconds(); day = tick.getDay(); month = tick.getMonth(); date = tick.getDate(); year = tick.getYear(); current = + ((hours >12) ? hours -12 :hours) current += ((minutes < 10) ? :0 : :) + minutes current += ((seconds < 10) ? :0 : :) + seconds current += (hours >= 12) ? P.M. : A.M. if(day==0){var weekday = 星期日} if(day==1){var weekday = 星期一} if(day==2){var weekday = 星期二} if(day==3){var weekday = 星期三} if(day==4){var weekday = 星期四} if(day==5){var weekday = 星期五} if(day==6){var weekday = 星期六} current +=(weekday) window.status=current; timeID = setTimeout(time(),1000); timeRunning = true; } function run(){ stop(); time(); } //--> </SCRIPT>
七、显示最后更新时间代码 如:最后更新时间: 11/27/2008 14:21:04
XML/HTML代码 <SCRIPT language=javascript> <!--hide script from old browsers document.write(最后更新时间: + document.lastModified + ) // end hiding --> </SCRIPT>
八、实时走动的数字时钟 如:下午14:22:03
XML/HTML代码 <script> function tick() { var hours, minutes, seconds, xfile; var intHours, intMinutes, intSeconds; var today; today = new Date(); intHours = today.getHours(); intMinutes = today.getMinutes(); intSeconds = today.getSeconds(); if (intHours == 0) { hours = 12:; xfile = 午夜; } else if (intHours < 12) { hours = intHours+:; xfile = 上午; } else if (intHours == 12) { hours = 12:; xfile = 正午; } else { intHoursintHours = intHours - 12 hours = intHours + :; xfile = 下午; } if (intMinutes < 10) { minutes = 0+intMinutes+:; } else { minutes = intMinutes+:; } if (intSeconds < 10) { seconds = 0+intSeconds+ ; } else { seconds = intSeconds+ ; } timeString = xfile+hours+minutes+seconds; Clock.innerHTML = timeString; window.setTimeout(tick();, 100); } window.onload = tick; </script> <div id=Clock style=font-size: 20; color:#000000></div>
九、根据不同的时间显示不同的问候语 如:午安!
XML/HTML代码 方法:在主页中你需要的地方加入以下代码: <script language=LiveScript> now = new Date(),hour = now.getHours() if(hour < 6){document.write(明天不用上班了吗?)} else if (hour < 8){document.write(全新的一天!)} else if (hour < 12){document.write(早安!)} else if (hour < 14){document.write(外面太阳大吗?)} else if (hour < 18){document.write(午安!)} else if (hour < 22){document.write(晚上好!)} else if (hour < 24){document.write(夜深了! 要注意身体呀! 祝你做个好梦!)} // --> </script> 说明,可以自行修改出更好更多的问候语!
十、显示年月日星期及农历的月和日 如:2008年11月27日 星期四 农历 10月30日
第一步:将一下代码保存为.JS文件
XML/HTML代码 <!-- function CalConv() { FIRSTYEAR = 1998; LASTYEAR = 2031; today = new Date(); SolarYear = today.getFullYear(); SolarMonth = today.getMonth() + 1; SolarDate = today.getDate(); Weekday = today.getDay(); LunarCal = [ new tagLunarCal( 27, 5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 ), new tagLunarCal( 46, 0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ), new tagLunarCal( 35, 0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ), new tagLunarCal( 23, 4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), new tagLunarCal( 42, 0, 1, 4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), new tagLunarCal( 31, 0, 2, 9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ), new tagLunarCal( 21, 2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), new tagLunarCal( 39, 0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ), new tagLunarCal( 28, 7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), new tagLunarCal( 48, 0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ), new tagLunarCal( 37, 0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ), |