当前位置:编程学习 > JS >>

用全中文在网页上显示日期或时间

用全中文在网页上显示日期和时间,用字符串替换技术去实现这一功能,其实大可不必用中文显示,有时候并不适合我们的阅读习惯,这里仅是一个教程实例,介绍字符串处理的一种方法,相信对JavaScript初学者还是有用的。
答案:<html>
<head>
<title>全中文日期显示</title>
</head>
<body>
<script language="javascript">
<!--
	str = new Array("零","一","二","三","四","五","六","七","八","九");
	function number(num){
		tostr = "" + num;
		if(num < 10 ){
			return str[num];
		}else if(num == 10){
			return "十";
		}else if(num < 20){
			return "十" + str[tostr.charAt(1)];
		}else if(num == 20){
			return "二十";
		}else if(num <30){
			return "二十" + str[tostr.charAt(1)];
		}else if(num == 30){
			return "三十";
		}else if(num > 30){
			return "三十" + str[tostr.charAt(1)];
		}
	}
	
	d = new Date();
	year  = ""+d.getYear();
	month = d.getMonth() + 1;
	date  = d.getDate();
	document.write(str[year.charAt(0)]+str[year.charAt(1)]+str[year.charAt(2)]+str[year.charAt(3)]+"年");
	document.write(number(month)+"月"+number(date)+"日");
//-->
</script>
</body>
</html>

上一个:在页面指定位置显示时钟
下一个:JavaScript获取今天是星期几

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,