数字型的日期转换为中文格式日期
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication
{
/// <summary>
/// 将数字型的日期转换为中文格式日期
/// "2004-7-12"转换为"二○○四年七月十二日"
/// </summary>
public class DateFormatChange
{
private static string[] chinese = new string[10]{"○","一","二","三","四","五","六","七","八","九"};
private static string[] len = new string[1]{"十"};
private static string[] ydm =new string[3]{"年","月","日"};
private const string REGEXP_IS_VALID_DATE = @"(\d{2}|\d{4})(\/|-)(\d{1,2})(\2)(\d{1,2})";
public DateFormatChange(){}
private string num2chinese(String s)
{
//将单个数字转成中文.
int slen = s.Length;
string result="";
for(int i=0;i<slen;i++)
{
string xxs = Convert.ToString(s[i]);
int jj = Convert.ToInt16(xxs);
result+=chinese[jj];
}
return result;
}
private string n2c(string s)
{
//对特殊情况进行处理.
if(s.Length==2)
{
if(s[0].Equals('1'))
{
if(s[1].Equals('0'))return len[0];
string xxs = Convert.ToString(s[1]);
int jj = Convert.ToInt16(xxs);
return len[0]+chinese[jj];
}
if(s[0].Equals('0'))
{
string xxs = Convert.ToString(s[1]);
int jj = Convert.ToInt16(xxs);
return chinese[jj];
}
if(s[1].Equals('0'))return chinese[s[0]]+len[0];
string xxs0 = Convert.ToString(s[0]);
int jj0 = Convert.ToInt16(s[0]);
string xxs1 = Convert.ToString(s[1]);
int jj1 = Convert.ToInt16(s[1]);
return chinese[jj0]+len[0]+chinese[jj1];
}
return num2chinese(s);
}
public string date2chinese(string s)
{
Match ma = Regex.Match(s,REGEXP_IS_VALID_DATE);
GroupCollection gc = ma.Groups;
string ok = "";
int count = gc.Count;
for (int i = 1; i < count; i=i+2)
{
ok+=n2c(gc[i].Value)+ydm[(i-1)/2];
}
return ok;
}
static void Main(string[] args)
{
DateFormatChange dfc = new DateFormatChange();
string shortDate = System.DateTime.Today.ToString("yyyy/MM/dd");
string CHNDate = dfc.date2chinese(shortDate);
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(shortDate);
Console.WriteLine(CHNDate);
}
}
}