作者zeat (Lucifer)
看板C_Sharp
标题Re: [问题] 怎麽输出中文数字日期?
时间Wed May 12 17:24:43 2010
petrushka:
首先很感谢你的提示. 的确, 我是自己转换, 但是是笨一点的方法. 虽然我想参考
你去修改"日", e.g. "一"到"三十一"之类的, 但年我就不知道了, 感觉上年并不像"日
"与"月"有个既定的循环, 於是我采取了.NET的Microsoft Visual Studio
International Feature Pack 2.0将阿拉伯数字转成国字来处理"年"与"日"的转换.
至於月份跟星期都可以拿现成的XD
我也放上我的code(请鞭小力QwQ一点):
string year, month, day, weekName;
//利用DateTimeFormatInfo可以现成的取得月份与星期
DateTimeFormatInfo dtfi = new CultureInfo("zh-TW").DateTimeFormat;
month = dtfi.GetMonthName(DateTime.Now.Month);
weekName = dtfi.GetDayName(DateTime.Now.DayOfWeek);
//"年"跟"日"是采用EastAsiaNumericFormatter转换
TaiwanCalendar tc = new TaiwanCalendar();
year = EastAsiaNumericFormatter.FormatWithCulture("Ln",
tc.GetYear(DateTime.Now), null, new CultureInfo("zh-TW"));
day = EastAsiaNumericFormatter.FormatWithCulture("Ln",
DateTime.Now.Day, null, new CultureInfo("zh-TW"));
最後再串联起来.
不过我知道我的方法很笨啦Orz… 但几行code就可以达到我想要的效果, 对於我这
个懒人来讲, C/P值很高:)
※ 引述《petrushka (不放过自己)》之铭言:
: 首先讲比较简单的,如果我要输出如"中华民国99年5月7日"怎麽做,范例如下:
: using System.Globalization;
: using System.Threading;
: static void Main( string[] args )
: {
: Thread.CurrentThread.CurrentCulture = new CultureInfo( "zh-TW" );
: TaiwanCalendar twCalendar = new TaiwanCalendar();
: Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar = twCalendar;
: Console.WriteLine( DateTime.Now.ToLongDateString() );
: Console.ReadKey();
: }
: 当前3行设定完成,基本上日期输出都会以中华民国历去显示。
: 再讲其他的,如果我要显示农历日期怎麽做,我贴上我使用的范例,如下:
: // 天干
: private static string[] CelestialStem = { string.Empty, "甲", "乙", "丙",
: "丁", "戊", "己", "庚", "辛", "壬", "癸" };
: // 地支
: private static string[] TerrestrialBranch = { string.Empty, "子", "丑", "寅",
: "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
: // 生肖
: private static string[] LunisolarSymbol = { string.Empty, "鼠", "牛", "虎",
: "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
: // 月份
: private static string[] LunisolarMonth = { string.Empty, "一", "二", "三",
: "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
: // 日
: private static string[] LunisolarDay = {string.Empty, "初一", "初二", "初三",
: "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二",
: "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一",
: "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "卅",
: "卅一" };
: // 取得台湾农历日期
: public static string GetTaiwanLunisolarDate( DateTime pDateTime )
: {
: // 农历
: TaiwanLunisolarCalendar chnCalendar = new TaiwanLunisolarCalendar();
: int sexagenaryYear = chnCalendar.GetSexagenaryYear( pDateTime );
: int celestialStemYear = chnCalendar.GetCelestialStem( sexagenaryYear );
: int terrestrialBranchYear = chnCalendar.GetTerrestrialBranch(
: sexagenaryYear );
: int chnMonth = chnCalendar.GetMonth( pDateTime );
: int leapMonth = chnCalendar.GetLeapMonth( chnCalendar.GetYear(
: pDateTime ) );
: int chnDay = chnCalendar.GetDayOfMonth( pDateTime );
: // 如果闰月
: if( leapMonth > 0 && chnMonth >= leapMonth )
: {
: chnMonth -= 1;
: }
: string lunisolarDate = string.Format(
: "{0}年{1}月{2}日",
: CelestialStem[ celestialStemYear ] +
: TerrestrialBranch[ terrestrialBranchYear ],
: LunisolarMonth[ chnMonth ],
: LunisolarDay[ chnDay ] );
: return lunisolarDate;
: }
: // 取得台湾农历生肖
: public static string GetTaiwanLunisolarSymbol( DateTime pDateTime )
: {
: // 农历
: TaiwanLunisolarCalendar chnCalendar = new TaiwanLunisolarCalendar();
: int sexagenaryYear = chnCalendar.GetSexagenaryYear( pDateTime );
: int terrestrialBranchYear = chnCalendar.GetTerrestrialBranch(
: sexagenaryYear );
: return LunisolarSymbol[ terrestrialBranchYear ];
: }
: 最後,你原文想要问的中文月份与星期,就只有采用类似农历日期的显示做法了,
: 就是自己去做出数字对中文的对应。
: .NET有提供中华民国(TaiwanCalendar)与农历(TaiwanLunisolarCalendar)的日历类别,
: 但是函式的回传值也还是int,这是因为类别介面要相容,且为了方便运算。
: 电脑运算都是数值,中文文字都是人家写好帮你对应的。.NET没有内建,当然就自己写。
: 别忘了,你是个程式设计师!
: 判断一下CurrentCulture,就可以去改写与调用自己的类别与函式。
: 就好像.NET有提供TaiwanCalendar,既然它功能不全,就再自己写一个新的!
: 譬说就叫NewTaiwanCalendar类别,这样就可以指派给CurrentCulture去使用。
: 最後就可以输出自己想要的。
: ※ 引述《zeat (Lucifer)》之铭言:
: : 各位好:
: : 我的问题是C#是否可以输出中文数字的日期, e.g. 九十九年四月二十五日.而不是
: : 99/4/25.
: : 虽然我有试过System.Globalization.CultureInfo, 但只有研究出GetMonthName
: : 跟GetDayName可以输出中文的月份跟星期, 年跟日就无法度了.
: : thanks a lot.
--
头发的长度决定威能的强度. by 小杰 富力士
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 203.68.64.24
1F:推 petrushka:因为「懒」,所以才要写程式自动化处理,这是动力呀~ 05/12 17:42
2F:→ petrushka:资料能正确呈现就好,不需什麽困难的演算, 没有好与坏~ 05/12 17:43