using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("請輸入一個日期:"); string strDate = Console.ReadLine(); string dc = Baodate2Chinese(strDate); Console.WriteLine(dc); } private static string Baodate2Chinese(string strDate) { char[] strChinese= new char[] { '〇','一','二','三','四','五','六','七','八','九','十' }; StringBuilder result = new StringBuilder(); //// 依據正則表達式判斷參數是否正確 //Regex theReg = new Regex(@"(d{2}|d{4})(/|-)(d{1,2})(/|-)(d{1,2})"); if (!string.IsNullOrEmpty(strDate)) { // 將數字日期的年月日存到字符數組str中 string[] str = null; if (strDate.Contains("-")) { str = strDate.Split('-'); } else if (strDate.Contains("/")) { str = strDate.Split('/'); } // str[0]中爲年,將其各個字符轉換爲相應的漢字 for (int i = 0; i < str[0].Length; i++) { result.Append(strChinese[int.Parse(str[0][i].ToString())]); } result.Append("年"); // 轉換月 int month = int.Parse(str[1]); int MN1 = month / 10; int MN2 = month % 10; if (MN1 > 1) { result.Append(strChinese[MN1]); } if (MN1 > 0) { result.Append(strChinese[10]); } if (MN2 != 0) { result.Append(strChinese[MN2]); } result.Append("月"); // 轉換日 int day = int.Parse(str[2]); int DN1 = day / 10; int DN2 = day % 10; if (DN1 > 1) { result.Append(strChinese[DN1]); } if (DN1 > 0) { result.Append(strChinese[10]); } if (DN2 != 0) { result.Append(strChinese[DN2]); } result.Append("日"); } else { throw new ArgumentException(); } return result.ToString(); } } }