// c#时间格式转换汉字大写 比如: “2011年6月4日” 转换成 “二零一一年六月四日”。
#region
public enum Years //枚举一个年份
{
一, 二, 三, 四, 五, 六, 七, 八, 九, 零
}
public enum Months //枚举一个月份1-12
{
一, 二, 三, 四, 五, 六, 七, 八, 九, 十, 十一, 十二
}
public enum Days //枚举一个日份
{
一, 二, 三, 四, 五, 六, 七, 八, 九, 十, 十一, 十二, 十三, 十四, 十五, 十六, 十七,
十八, 十九, 二十, 二十一, 二十二, 二十三, 二十四, 二十五, 二十六, 二十七, 二十八, 二十九, 三十, 三十一
}
#endregion
/*也可以枚举一些年来用switch取出来,但不是很多,因为我们不可能写上千个枚举的数!!
public enum YY
{
二零一零,二零二,二零三零,二零四零,二零五零,二零六零,二零七零,二零八零,二零九零
} 该方法没有写出来 */
static void Main(string[] args)
{
//计算年份
Console.WriteLine("请输入转换的年份,1000-9999");
string years = Console.ReadLine(); //接收年份
char[] char_years = new char[4]; //把输入的年份拆成每一个字符数字的数组
string year = "";
int i = 0;
for (i = 0; i < char_years.Length;i++ )
{
char_years[i] = Convert.ToChar(years.Substring(i, 1)); //取出千百十个位的字符
#region
//分别转换他们
switch (char_years[i])
{
case '1':
year = Convert.ToString(Years.一);
break;
case '2':
year = Convert.ToString(Years.二);
break;
case '3':
year = Convert.ToString(Years.三);
break;
case '4':
year = Convert.ToString(Years.四);
break;
case '5':
year = Convert.ToString(Years.五);
break;
case '6':
year = Convert.ToString(Years.六);
break;
case '7':
year = Convert.ToString(Years.七);
break;
case '8':
year = Convert.ToString(Years.八);
break;
case '9':
year = Convert.ToString(Years.九);
break;
default:
year = Convert.ToString(Years.零);
break;
}
#endregion
Console.Write(year);
}
Console.Write("年"); //这个年写在这里是有原因的,因为上面是for循环,假许写在上面将会每输出一个字符跟一个年
Console.WriteLine("\t");
//计算月份
Console.WriteLine("请输入转换的月份");
string months = Console.ReadLine();
string month = "";
#region
switch (months)
{
case "1":
month = Convert.ToString(Months.一);
break;
case "2":
month = Convert.ToString(Months.二);
break;
case "3":
month = Convert.ToString(Months.三);
break;
case "4":
month = Convert.ToString(Months.四);
break;
case "5":
month = Convert.ToString(Months.五);
break;
case "6":
month = Convert.ToString(Months.六);
break;
case "7":
month = Convert.ToString(Months.七);
break;
case "8":
month = Convert.ToString(Months.八);
break;
case "9":
month = Convert.ToString(Months.九);
break;
case "10":
month = Convert.ToString(Months.十);
break;
case "11":
month = Convert.ToString(Months.十一);
break;
case "12":
month = Convert.ToString(Months.十二);
break;
default:
Console.WriteLine("输入的格式错误,月份只有1-12");
break;
}
#endregion
Console.WriteLine("{0}月", month);
//计算日份
Console.WriteLine("请输入转换的日份");
string days = Console.ReadLine();
string day = "";
#region
switch (days)
{
case "1":
day = Convert.ToString(Days.一);
break;
case "2":
day = Convert.ToString(Days.二);
break;
case "3":
day = Convert.ToString(Days.三);
break;
case "4":
day = Convert.ToString(Days.四);
break;
case "5":
day = Convert.ToString(Days.五);
break;
case "6":
day = Convert.ToString(Days.六);
break;
case "7":
day = Convert.ToString(Days.七);
break;
case "8":
day = Convert.ToString(Days.八);
break;
case "9":
day = Convert.ToString(Days.九);
break;
case "10":
day = Convert.ToString(Days.十);
break;
case "11":
day = Convert.ToString(Days.十一);
break;
case "12":
day = Convert.ToString(Days.十二);
break;
case "13":
day = Convert.ToString(Days.十三);
break;
case "14":
day = Convert.ToString(Days.十四);
break;
case "15":
day = Convert.ToString(Days.十五);
break;
case "16":
day = Convert.ToString(Days.十六);
break;
case "17":
day = Convert.ToString(Days.十七);
break;
case "18":
day = Convert.ToString(Days.十八);
break;
case "19":
day = Convert.ToString(Days.十九);
break;
case "20":
day = Convert.ToString(Days.二十);
break;
case "21":
day = Convert.ToString(Days.二十一);
break;
case "22":
day = Convert.ToString(Days.二十二);
break;
case "23":
day = Convert.ToString(Days.二十三);
break;
case "24":
day = Convert.ToString(Days.二十四);
break;
case "25":
day = Convert.ToString(Days.二十五);
break;
case "26":
day = Convert.ToString(Days.二十六);
break;
case "27":
day = Convert.ToString(Days.二十七);
break;
case "28":
day = Convert.ToString(Days.二十八);
break;
case "29":
day = Convert.ToString(Days.二十九);
break;
case "30":
day = Convert.ToString(Days.三十);
break;
case "31":
day = Convert.ToString(Days.三十一);
break;
default:
Console.WriteLine("输入的格式错误,日份1-31");
break;
}
#endregion
Console.WriteLine("{0}月{1}日", month, day);
Console.ReadKey();
}
想看其他的解法!!!! |
|