<html>
<title>digit clock</title>
<head>
<script type="text/javascript">
function init(){
var clock = document.getElementById("clock");
setInterval("getCurrentTime()",1000);
}
function getCurrentTime(){
var now = new Date();
var stringBuffer = new StringBuffer();
stringBuffer.append(getYear(now));
stringBuffer.append(getMonth(now));
stringBuffer.append(getDay(now));
stringBuffer.append(getTime(now));
clock.innerHTML = stringBuffer.toString();
}
function getYear(now){
return now.getYear()+"年";
}
function getMonth(now){
var month = now.getMonth()+1;
if(month<10)
month = "0"+month;
return month +"月";
}
function getDate(now){
var date = now.getDate();
if(date < 10)
date = "0" + date;
return date + "日";
}
function getDay(now){
var day = now.getDay();
switch(day){
case 0:return "星期天";
case 1:return "星期一";
case 2:return "星期二";
case 3:return "星期三";
case 4:return "星期四";
case 5:return "星期五";
case 6:return "星期六";
}
}
function getTime(now){
var hours= now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
if(hours<10)
hours = "0"+hours;
if(minutes<10)
minutes = "0"+minutes;
if(seconds<10)
seconds = "0"+seconds;
return hours+":"+minutes+":"+seconds;
}
function StringBuffer(){
this.strings = new Array();
}
StringBuffer.prototype.append = function(str){
this.strings.push(str);
}
StringBuffer.prototype.toString = function(){
return this.strings.join("");
}
</script>
</head>
<body onload="init();">
<div id="clock" name="clock"/>
</body>
</html>
js创建一个StringBuffer
最新推荐文章于 2023-04-25 15:45:55 发布