当前位置: 代码迷 >> Web前端 >> 腾讯的面试题:将日期的展示转换成中文显示
  详细解决方案

腾讯的面试题:将日期的展示转换成中文显示

热度:309   发布时间:2012-10-25 10:58:57.0
腾讯的面试题:将日期的显示转换成中文显示
我的腾讯一面时候的面试题,将日期转换成中文显示出来,当时做的时候采用了比较笨的方法,挨着去转换,回来仔细想了想采用
了这种办法来实现,代码量减少了许多!有什么问题欢迎留言探讨
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">  
<style type="text/css">
#play {
	background-color:yellow;
	width:200px;
	height:60px;
	color:blue;
	border:1px solid black;
}
</style>
 </HEAD>

 <BODY>
	<div id="play">
		
	</div>
  <script type="text/javascript">
	 window.onload = function() {
		start();
	 }
	 function start() {
		setInterval("setTime()",1000);
	 }
	 function setTime() {
		var date = new Date();
		var year = date.getFullYear().toString();
		var month = (date.getMonth()+1).toString();
		var day = date.getDate().toString();
		var hour = date.getHours().toString();
		var minute = date.getMinutes().toString();
		var second = date.getSeconds().toString();		
		if(month.length >1) {
			if(month.charAt(1) != "0") {
				month = month.charAt(0)+","+month.charAt(1);
			} else {
				month = month.charAt(0)+",";
			}
		}
		if(day.length > 1) {
			if(day.charAt(1) != "0") {
				day = day.charAt(0)+","+day.charAt(1);
			} else {
				day = day.charAt(0)+",";
			}
		}
		if(hour.length > 1) {
			if(hour.charAt(1) != "0") {
				hour = hour.charAt(0)+","+hour.charAt(1);
			} else {
				hour = hour.charAt(0)+",";
			}
		}
		if(minute.length > 1) {
			if(minute.charAt(1) != "0") {
				minute = minute.charAt(0)+","+minute.charAt(1);
			} else {
				minute = minute.charAt(0)+",";
			}
		}
		if(second.length > 1) {
			if(second.charAt(1) != "0") {
				second = second.charAt(0)+","+second.charAt(1);
			} else {
				second = second.charAt(0)+",";
			}
		} else {
			second = "0"+second;
		}
		var newYear = "";
		for(var i = 0;i < year.length;i++) {
			var ch = year.charAt(i);
			newYear += toChinese(ch);
		}
		var newMonth = "";
		for(var i = 0;i < month.length;i++) {
			var ch = month.charAt(i);
			newMonth += toChinese(ch);
		}
		var newDay = "";
		for(var i = 0;i < day.length;i++) {
			var ch = day.charAt(i);
			newDay += toChinese(ch);
		}
		var newHour = "";
		for(var i = 0;i < hour.length;i++) {
			var ch = hour.charAt(i);
			newHour += toChinese(ch);
		}
		var newMinute = "";
		for(var i = 0;i < minute.length;i++) {
			var ch = minute.charAt(i);
			newMinute += toChinese(ch);
		}
		var newSecond = "";
		for(var i = 0;i < second.length;i++) {
			var ch = second.charAt(i);
			newSecond += toChinese(ch);
		}
		var str = newYear+"年"+newMonth+"月"+newDay+"日<br/>";
			str += newHour +"时"+newMinute+"分"+newSecond+"秒";
		document.getElementById("play").innerHTML = str;
	 }

	 function toChinese(source) {
		var result = "";
		switch(source) {
			case "1" :
				result = "一";
				break;
			case "2" :
				result = "二";
				break;
			case "3" :
				result = "三";
				break;
			case "4" :
				result = "四";
				break;
			case "5" :
				result = "五";
				break;
			case "6" :
				result = "六";
				break;
			case "7" :
				result = "七";
				break;
			case "8" :
				result = "八";
				break;
			case "9" :
				result = "九";
				break;
			case "0" :
				result = "零";
				break;
			case "," :
				result = "十";
				break;
		}
		return result;
	 }
  </script>
 </BODY>
</HTML>
?
  相关解决方案