js StringBuffer 만들기

2199 단어 StringBuffer

<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>


좋은 웹페이지 즐겨찾기