C_Sharp 板


LINE

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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP