C# 获取农历日期
2014-01-06 18:32
459 查看
//C# 获取农历日期 ///<summary> /// 实例化一个 ChineseLunisolarCalendar ///</summary> private static ChineseLunisolarCalendar ChineseCalendar =new ChineseLunisolarCalendar(); ///<summary> /// 十天干 ///</summary> private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; ///<summary> /// 十二地支 ///</summary> private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; ///<summary> /// 十二生肖 ///</summary> private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; ///<summary> /// 返回农历天干地支年 ///</summary> ///<param name="year">农历年</param> ///<return s></return s> public static string GetLunisolarYear(int year) { if (year >3) { int tgIndex = (year -4) %10; int dzIndex = (year -4) %12; return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]"); } throw new ArgumentOutOfRangeException("无效的年份!"); } ///<summary> /// 农历月 ///</summary> ///<return s></return s> private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" }; ///<summary> /// 农历日 ///</summary> private static string[] days1 = { "初", "十", "廿", "三" }; ///<summary> /// 农历日 ///</summary> private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; ///<summary> /// 返回农历月 ///</summary> ///<param name="month">月份</param> ///<return s></return s> public static string GetLunisolarMonth(int month) { if (month <13 && month >0) { return months[month -1]; } throw new ArgumentOutOfRangeException("无效的月份!"); } ///<summary> /// 返回农历日 ///</summary> ///<param name="day">天</param> ///<return s></return s> public static string GetLunisolarDay(int day) { if (day >0 && day <32) { if (day !=20 && day !=30) { return string.Concat(days1[(day -1) /10], days[(day -1) %10]); } else { return string.Concat(days[(day -1) /10], days1[1]); } } throw new ArgumentOutOfRangeException("无效的日!"); } ///<summary> /// 根据公历获取农历日期 ///</summary> ///<param name="datetime">公历日期</param> ///<return s></return s> public static string GetChineseDateTime(DateTime datetime) { int year = ChineseCalendar.GetYear(datetime); int month = ChineseCalendar.GetMonth(datetime); int day = ChineseCalendar.GetDayOfMonth(datetime); //获取闰月, 0 则表示没有闰月 int leapMonth = ChineseCalendar.GetLeapMonth(year); bool isleap =false; if (leapMonth >0) { if (leapMonth == month) { //闰月 isleap =true; month--; } else if (month > leapMonth) { month--; } } return string.Concat(GetLunisolarYear(year), "年", isleap ?"闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day)); }
相关文章推荐
- C# 获取农历日期
- C# 获取农历日期
- c#获取系统日期,星期几
- C# 获取系统日期、时间
- 计算一年中的第几周/c#得到阳历对应农历日期
- C#获取当前时间、日期
- C# 获取当前日期(转)
- C#获取当前日期时间(转)
- C#获取当前日期时间【转】
- C# 获取当前年份的周期及周期所在日期范围(推荐)
- C#获取上个月第一天和最后一天日期的方法
- DateTime日期格式获取 分类: C# 2014-04-15 10:36 233人阅读 评论(0) 收藏
- C#实战小技巧(四):获取当前日期时间
- C#获取日期的星期名称实例代码
- iOS 获取公历、农历日期的年月日的实例代码
- [转载]获取当前日期和农历的js代码
- c#获取当前日期时间(转)
- 获取农历日期
- C#获取下一个月的1号的日期
- C# 获取当前月第一天和最后一天 计算两个日期差多少天