数字型的日期转换为中文格式日期

yourtomato 2004-07-03 04:07:31
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);
}

}
}
...全文
368 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liwei1981116 2004-07-08
  • 打赏
  • 举报
回复
我用了你的这个,很好用,谢谢!!!!!!!
sarcophile 2004-07-03
  • 打赏
  • 举报
回复
好。虽然我用不到,还是帮你顶。
yourtomato 2004-07-03
  • 打赏
  • 举报
回复
最近做公文处理,客户要求公文的日期格式要是全中文形式的,即"二○○四年七月十二日"
到网上查了查,只有Javascript写的,就拿过来改改,变成了这个.net版的,希望有人能用到。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧