C#數字日期裝換為中文日期,
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 namespace ConsoleApplication1
6 {
7 class Program
8 {
9
10 static void Main(string[] args)
11 {
12 Console.WriteLine("請輸入一個日期:");
13 string strDate = Console.ReadLine();
14 string dc = Baodate2Chinese(strDate);
15 Console.WriteLine(dc);
16 }
17
18 private static string Baodate2Chinese(string strDate)
19 {
20 char[] strChinese= new char[] {
21 '〇','一','二','三','四','五','六','七','八','九','十'
22 };
23 StringBuilder result = new StringBuilder();
24
25 //// 依據正則表達式判斷參數是否正確
26 //Regex theReg = new Regex(@"(d{2}|d{4})(/|-)(d{1,2})(/|-)(d{1,2})");
27
28 if (!string.IsNullOrEmpty(strDate))
29 {
30 // 將數字日期的年月日存到字符數組str中
31 string[] str = null;
32 if (strDate.Contains("-"))
33 {
34 str = strDate.Split('-');
35 }
36 else if (strDate.Contains("/"))
37 {
38 str = strDate.Split('/');
39 }
40 // str[0]中為年,將其各個字符轉換為相應的漢字
41 for (int i = 0; i < str[0].Length; i++)
42 {
43 result.Append(strChinese[int.Parse(str[0][i].ToString())]);
44 }
45 result.Append("年");
46
47 // 轉換月
48 int month = int.Parse(str[1]);
49 int MN1 = month / 10;
50 int MN2 = month % 10;
51
52 if (MN1 > 1)
53 {
54 result.Append(strChinese[MN1]);
55 }
56 if (MN1 > 0)
57 {
58 result.Append(strChinese[10]);
59 }
60 if (MN2 != 0)
61 {
62 result.Append(strChinese[MN2]);
63 }
64 result.Append("月");
65
66 // 轉換日
67 int day = int.Parse(str[2]);
68 int DN1 = day / 10;
69 int DN2 = day % 10;
70
71 if (DN1 > 1)
72 {
73 result.Append(strChinese[DN1]);
74 }
75 if (DN1 > 0)
76 {
77 result.Append(strChinese[10]);
78 }
79 if (DN2 != 0)
80 {
81 result.Append(strChinese[DN2]);
82 }
83 result.Append("日");
84 }
85 else
86 {
87 throw new ArgumentException();
88 }
89
90 return result.ToString();
91 }
92 }
93 }
View Code