今天在写代码时遇到一个需要将日期转换为中文大写日期的问题,网上找了找,示例不是很多,只有javascript代码的,以下是这个链接(http://www.cnblogs.com/isme/articles/267043.html),把它转换为C#版贴出来与大家分享
C#版
/// <summary>
/// 功能:YYYY-MM-DD 数字日期转化为汉字
/// 例:1984-3-7 -> 一九八四年三月七日
/// 调用:Format("1984-3-7")
/// </summary>
public class DateTimeEx
{
string[] chinese = new string[10] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
string seperator = "十";
string[] ydm = new string[3] { "年", "月", "日" };
public DateTimeEx()
{
}
public string Format(DateTime date)
{
string result = String.Empty;
//验证输入的日期格式.并提取相关数字.
if (date != null)
{
string year = date.Year.ToString();
string month = date.Month.ToString();
string day = date.Day.ToString();
//年
result = Num2Chinese(year);
//月
result += "年" + Num2Chinese(month);
//日
result += "月" + Num2Chinese(day);
}
return result;
}
string Num2Chinese(string s)
{
//对特殊情况进行处理.
if (s.Length == 2)
{
if (s[0].Equals('1'))
{
if (s[1].Equals('0'))
{
return seperator;
}
return seperator + chinese[Int32.Parse(s[1].ToString())];
}
if (s[1].Equals('0'))
{
return chinese[Int32.Parse(s[0].ToString())] + seperator;
}
return chinese[Int32.Parse(s[0].ToString())] + seperator + chinese[Int32.Parse(s[1].ToString())];
}
return Num2Char(s);
}
string Num2Char(string s)
{
//将单个数字转成中文.
int slen = s.Length;
var result = String.Empty;
for (var i = 0; i < slen; i++)
{
result += chinese[Int32.Parse(s[i].ToString())];
}
return result;
}
}
/// <summary>
/// 功能:YYYY-MM-DD 数字日期转化为汉字
/// 例:1984-3-7 -> 一九八四年三月七日
/// 调用:Format("1984-3-7")
/// </summary>
public class DateTimeEx
{
string[] chinese = new string[10] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
string seperator = "十";
string[] ydm = new string[3] { "年", "月", "日" };
public DateTimeEx()
{
}
public string Format(DateTime date)
{
string result = String.Empty;
//验证输入的日期格式.并提取相关数字.
if (date != null)
{
string year = date.Year.ToString();
string month = date.Month.ToString();
string day = date.Day.ToString();
//年
result = Num2Chinese(year);
//月
result += "年" + Num2Chinese(month);
//日
result += "月" + Num2Chinese(day);
}
return result;
}
string Num2Chinese(string s)
{
//对特殊情况进行处理.
if (s.Length == 2)
{
if (s[0].Equals('1'))
{
if (s[1].Equals('0'))
{
return seperator;
}
return seperator + chinese[Int32.Parse(s[1].ToString())];
}
if (s[1].Equals('0'))
{
return chinese[Int32.Parse(s[0].ToString())] + seperator;
}
return chinese[Int32.Parse(s[0].ToString())] + seperator + chinese[Int32.Parse(s[1].ToString())];
}
return Num2Char(s);
}
string Num2Char(string s)
{
//将单个数字转成中文.
int slen = s.Length;
var result = String.Empty;
for (var i = 0; i < slen; i++)
{
result += chinese[Int32.Parse(s[i].ToString())];
}
return result;
}
}
顺便把原Javascript版贴来
JavaScript版
<script>
/*
功能:YYYY-MM-DD 数字日期转化为汉字
例:1984-3-7 -> 一九八四年三月七日
调用:baodate2chinese("1984-3-7")
*/
var chinese = ['零','一','二','三','四','五','六','七','八','九'];
var len = ['十'];
var ydm =['年','月','日'];
function num2chinese(s)
{
//将单个数字转成中文.
s=""+s;
slen = s.length;
var result="";
for(var i=0;i<slen;i++)
{
result+=chinese[s.charAt(i)];
}
return result;
}
function n2c(s)
{
//对特殊情况进行处理.
s=""+s;
var result="";
if(s.length==2)
{
if(s.charAt(0)=="1")
{
if(s.charAt(1)=="0")return len[0];
return len[0]+chinese[s.charAt(1)];
}
if(s.charAt(1)=="0")return chinese[s.charAt(0)]+len[0];
return chinese[s.charAt(0)]+len[0]+chinese[s.charAt(1)];
}
return num2chinese(s)
}
function baodate2chinese(s)
{
//验证输入的日期格式.并提取相关数字.
var datePat = /^(\d{2}|\d{4})(\/|-)(\d{1,2})(\2)(\d{1,2})$/;
var matchArray = s.match(datePat);
var ok="";
if (matchArray == null) return false;
for(var i=1;i<matchArray.length;i=i+2)
{
ok+=n2c(matchArray[i]-0)+ydm[(i-1)/2];
}
return ok;
}
</script>
YYYY-MM-DD:<input type=text name="mydate" value="1984-3-7">
中文显示:<input type=text name="okdate">
<input type=button onclick="document.all.okdate.value=baodate2chinese(document.all.mydate.value)"
value="转换">
/*
功能:YYYY-MM-DD 数字日期转化为汉字
例:1984-3-7 -> 一九八四年三月七日
调用:baodate2chinese("1984-3-7")
*/
var chinese = ['零','一','二','三','四','五','六','七','八','九'];
var len = ['十'];
var ydm =['年','月','日'];
function num2chinese(s)
{
//将单个数字转成中文.
s=""+s;
slen = s.length;
var result="";
for(var i=0;i<slen;i++)
{
result+=chinese[s.charAt(i)];
}
return result;
}
function n2c(s)
{
//对特殊情况进行处理.
s=""+s;
var result="";
if(s.length==2)
{
if(s.charAt(0)=="1")
{
if(s.charAt(1)=="0")return len[0];
return len[0]+chinese[s.charAt(1)];
}
if(s.charAt(1)=="0")return chinese[s.charAt(0)]+len[0];
return chinese[s.charAt(0)]+len[0]+chinese[s.charAt(1)];
}
return num2chinese(s)
}
function baodate2chinese(s)
{
//验证输入的日期格式.并提取相关数字.
var datePat = /^(\d{2}|\d{4})(\/|-)(\d{1,2})(\2)(\d{1,2})$/;
var matchArray = s.match(datePat);
var ok="";
if (matchArray == null) return false;
for(var i=1;i<matchArray.length;i=i+2)
{
ok+=n2c(matchArray[i]-0)+ydm[(i-1)/2];
}
return ok;
}
</script>
YYYY-MM-DD:<input type=text name="mydate" value="1984-3-7">
中文显示:<input type=text name="okdate">
<input type=button onclick="document.all.okdate.value=baodate2chinese(document.all.mydate.value)"
value="转换">