JS中文日期转换
charAt
charAt(int index)方法是一个能够用来检索特定引索下的字符的String实例的方法.
charAt()方法返回一个位于提供给它的参数引索处的字符.
如: str.chatAt(0)检索str中的第一个字符,str.charAt(str.length()-1)检索最后一个字符.
下面的示例阐释了 charAt 方法的用法:
<script language="javascript">
function charAtTest(n)
{
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";// Initialize variable.
var s; // Declare variable.
s = str.charAt(n - 1); // Get correct character // from position n – 1.
return(s); // Return character.
}
alert(s);
</script>
中文日期转换:
代码
<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)]+" 年 "+number(month)+" 月 "+number(date)+" 日 ");
//-->
</script>
</body>
</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)]+" 年 "+number(month)+" 月 "+number(date)+" 日 ");
//-->
</script>
</body>
</html>
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
<!--
function ShowDate(now){
var NumArr = ["○", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
alert(now.toLocaleString());
now = now.toLocaleString().split(" ")[0];
return now.replace(/(\d)/g, function(a, b){
return NumArr[b.charAt()];
}).replace(/年([^月]+)月([^日]+)/, function(a, b, c){
b = b.length > 1 ? ("十" + b.substr(1, 1).replace(/○/, "")) : b;
c = c.length > 1 ? (c.substr(0, 1).replace(/一/, "") + "十" + c.substr(1, 1).replace(/○/, "")) : c;
return "年" + b + "月" + c;
});
}
window.onload = function(){
document.getElementById("ximen").value = ShowDate(new Date());
}
//-->
</script>
</head>
<body>
<input type="text" id="ximen" value="" style="width:200px;">
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
<!--
function ShowDate(now){
var NumArr = ["○", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
alert(now.toLocaleString());
now = now.toLocaleString().split(" ")[0];
return now.replace(/(\d)/g, function(a, b){
return NumArr[b.charAt()];
}).replace(/年([^月]+)月([^日]+)/, function(a, b, c){
b = b.length > 1 ? ("十" + b.substr(1, 1).replace(/○/, "")) : b;
c = c.length > 1 ? (c.substr(0, 1).replace(/一/, "") + "十" + c.substr(1, 1).replace(/○/, "")) : c;
return "年" + b + "月" + c;
});
}
window.onload = function(){
document.getElementById("ximen").value = ShowDate(new Date());
}
//-->
</script>
</head>
<body>
<input type="text" id="ximen" value="" style="width:200px;">
</body>
</html>