(javascript、vbscript、C#、flash、C++、C等等)
WonSoft编
文章提供计算农历天干地支及当年属相的算法源程序,使用的语言为Javascript、VBScript、C#等。
一、C# 代码(1):
原来还准备自己写算法,并研究农历规则。发现那太难和麻烦了,光是农历的推算那就我等专门研究历法的人一下搞懂的。后来发现。NET类库也提供一些基础的农历类System.Globalization.ChineseLunisolarCalendar。我改装了一下如DateTime时间形式。代码如下。实现了 公历农历转换的功能。但是只能算到1900~2100年之间的。基本够日常使用了。源代码如下:
using System; using System.Collections.Generic; using System.Text; namespace System { //// <summary> /// 中国常用农历日期时间类 /// zj53hao@qq.com http://hi.csdn.net/zj53hao /// </summary> class ChinaDateTime { private int year, month, dayOfMonth; private bool isLeap; public DateTime time; //// <summary> /// 获取当前日期的农历年 /// </summary> public int Year { get { return year; } } //// <summary> /// 获取当前日期的农历月份 /// </summary> public int Month { get { return month; } } //// <summary> /// 获取当前日期的农历月中天数 /// </summary> public int DayOfMonth { get { return dayOfMonth; } } //// <summary> /// 获取当前日期是否为闰月中的日期 /// </summary> public bool IsLeap { get { return isLeap; } } System.Globalization.ChineseLunisolarCalendar cc; //// <summary> /// 返回指定公历日期的阴历时间 /// </summary> /// <param name="time"></param> public ChinaDateTime(DateTime time) { cc = new System.Globalization.ChineseLunisolarCalendar(); if (time > cc.MaxSupportedDateTime || time < cc.MinSupportedDateTime) throw new Exception("参数日期时间不在支持的范围内,支持范围:" + cc.MinSupportedDateTime.ToShortDateString()+"到"+cc.MaxSupportedDateTime.ToShortDateString()); year = cc.GetYear(time); month = cc.GetMonth(time); dayOfMonth = cc.GetDayOfMonth(time); isLeap = cc.IsLeapMonth(year, month); if (isLeap) month -= 1; this.time = time; } //// <summary> /// 返回当前日前的农历日期。 /// </summary> public static ChinaDateTime Now { get { return new ChinaDateTime(DateTime.Now); } } //// <summary> /// 返回指定农历年,月,日,是否为闰月的农历日期时间 /// </summary> /// <param name="Year"></param> /// <param name="Month"></param> /// <param name="DayOfMonth"></param> /// <param name="IsLeap"></param> public ChinaDateTime(int Year, int Month, int DayOfMonth, bool IsLeap) { if (Year >= cc.MaxSupportedDateTime.Year || Year <= cc.MinSupportedDateTime.Year) throw new Exception("参数年份时间不在支持的范围内,支持范围:" + cc.MinSupportedDateTime.ToShortDateString() + "到" + cc.MaxSupportedDateTime.ToShortDateString()); if (Month < 1 || Month > 12) throw new Exception("月份必须在1~12范围"); cc = new System.Globalization.ChineseLunisolarCalendar(); if(cc.GetLeapMonth(Year)!=Month&&IsLeap) throw new Exception("指定的月份不是当年的闰月"); if (cc.GetDaysInMonth(Year, IsLeap Month + 1 : Month) < DayOfMonth || DayOfMonth < 1) throw new Exception("指定的月中的天数不在当前月天数有效范围"); year = Year; month = Month; dayOfMonth = DayOfMonth; isLeap = IsLeap; time = DateTime.Now; } //// <summary> /// 获取当前农历日期的公历时间 /// </summary> public DateTime ToDateTime() { return cc.ToDateTime(year, isLeap month + 1 : month, dayOfMonth, time.Hour, time.Minute, time.Second, time.Millisecond); } //// <summary> /// 获取指定农历时间对应的公历时间 /// </summary> /// <param name="CnTime"></param> /// <returns></returns> public static DateTime ToDateTime(ChinaDateTime CnTime) { return CnTime.ToDateTime(); } //// <summary> /// 获取指定公历时间转换为农历时间 /// </summary> /// <param name="Time"></param> /// <returns></returns> public static ChinaDateTime ToChinaDateTime(DateTime Time) { return new ChinaDateTime(Time); } } }
二、C#代码(2):
1、农历类的使用
.net框架不支持直接将日期转换成农历格式的字符串,那么要将显示农历格式的日期,就只要自已写代码了。不过由于已经有了ChineseLunisolarCalendar类实现了公历转换为农历日期的功能,所以要写这样的代码也比较简单。需要用到ChineseLunisolarCalendar以下几个主要方法:
int GetYear (DateTime time) 获取指定公历日期的农历年份,使用的还是公历纪元。在每年的元旦之后春节之前农历的纪年会比公历小1,其它时候等于公历纪年。虽然农历使用传说中的耶稣生日纪元似乎不太妥当,不过我们确实已经几十年没有实行一个更好的纪年办法,也只有将就了。
int GetMonth (DateTime time) 获取指定公历日期的农历月份。这里要注意了,由于农历有接近三分之一的年份存在闰月,则在这些年份里会有十三个,而具体哪一个月是闰月也说不准,这里不同于希伯来历。以今年为例,今年闰七月,则此方法在参数为闰七月的日期是返回值为 8,参数为农历十二月的日期时返回值为13
bool IsLeapMonth ( int year, int month) 获取指定农历年份和月份是否为闰月,这个函数和上个函数配合使用就可以算出农历的月份了。
int GetDayOfMonth (DateTime time) 获取指定公历日期的农历天数,这个值根据大月或者小月取值是1到30或者1到29, MSDN上说的1到31显然是错的, 没有哪个农历月份会有31天。
int GetSexagenaryYear (DateTime time) 获取指定公历日期的农历年份的干支纪年,从1到60,分别是甲子、乙丑、丙寅、….癸亥, 比如戊戌变法、辛亥革命就是按这个来命名的。当然算八字也少不了这个。
int GetCelestialStem (int sexagenaryYear) 获取一个天支的天干, 从1到10, 表示甲、乙、丙….,说白了就是对10取模。
int GetTerrestrialBranch (int sexagenaryYear) ) 获取一个干支的地支,, 从1到12, 表示子、丑、寅、…今年是狗年,那么今年年份的地支就是“戌”。
有了这几个方法,显示某天的农历月份日期、农历节日等都是小菜一碟,算命先生排八字用这几个方法,又快又准确,写出的代码也很短。
2、几种东亚农历类的区别
经过我的测试,ChineseLunisolarCalendar, JapaneseLunisolarCalendar, KoreanLunisolaCalendarr, TaiwanLunisolarCalendar这四种日历,无论哪一种,以2006年2月6日为参数,调用它们的GetMonth方法得到的结果都是1,GetDayOfMonth得到的结果都是8。想想也是,我们过的端午节和韩国的不太可能不是一天。
但是调用GetYear方法得到结果就有区别了ChineseLunisolarCalendar和KoreanLunisolarCalendar都返回2006,也就是公历纪年,TaiwanLunisolarCalendar的返回值是95,依然是民国纪年,JapaneseLunisolarCalendar的返回值是18, 平成纪年。
另外的一个区别是这四种日历的MinSupportedDateTime和MaxSupportedDateTime各不一样,以下是对照表:
日历类 MinSupportedDateTime MaxSupportedDateTime
ChineseLunisolarCalendar 公元1901年1月初1 公元2100年12月29
TaiwanLunisolarCalendar 民国1年1月初1 民国139年12月29
JapaneseLunisolarCalendar 昭和35年1月初1 平成61年12月29
KoreanLunisolarCalendar 公元918年1月初1 公元2050年12月29
韩国农历类支持的最小日期为918年(也即高丽王朝建立的年份),以此而论,中国农历类支持的最小日期不说从商周算起,从汉唐算总该没问题吧?微软公司啊,又在“厚彼薄此”,唉。
其次,日本还以天皇纪年,如果哪天xxxx, 岂不是使用JapaneseLunisolarCalendar写出的程序都有问题啦?
3、写自已的日期格式化器
昨天看了一篇文章,说目前大家用的“农历”这个术语是文革时期才有的,目的是反封建。这里为了省事,还是继续使用这个术语。而英文名称ChineseLunisolarCalendar太长,我自己的代码中就用ChineseCalendar为相关功能命名,这个名字也还过得去吧。
我原先设想自定义一个类,使得能写出这样的代码:
string s= DateTime.Now.ToString(new MyFormatProvider());
就能得出我想要的农历日期字符串,经过测试却失败了,依据我的分析,微软公司在.net框架中把日期时间型的格式写死了,只能依据相关的地区采用固定的几种显示格式,没法再自行定义。而前文已经说过,而所有的相关格式微软公司都放到一个名为culture.nlp的文件中(这个文件在以前的.net框架是一个独立的文件,在.net 2.0被作为一个资源编译到mscorlib.dll中。) (我的这个不能为DateTime写自已的格式化器的观点没有资料佐证,如有不当之处,请大家指正)
虽然不能为DataTime写自定义的格式器,但还有另外一个途径,就是为String类的Format方法写自定义格式化器,我测试了一下,效果还不错,调用方式如下:
string s= String.Format(new ChineseCalendarFormatter(), "{0:D}",DateTime.Now);
可以得到“二〇〇六年正月初九”
string s= String.Format(new ChineseCalendarFormatter(), "{0:d}",DateTime.Now);
可以得到“丙戌年正月初九”
虽然没有前面所设想的方便,但也还能接受,全部代码帖出如下:
第一个类,主要是封装了农历的一些常用字符和对日历处理的最基本功能
using System; using System.Collections.Generic; using System.Text; using System.Globalization; public static class ChineseCalendarHelper { public static string GetYear(DateTime time) { StringBuilder sb = new StringBuilder(); int year = calendar.GetYear(time); int d; do { d = year % 10; sb.Insert(0, ChineseNumber[d]); year = year / 10; } while (year > 0); return sb.ToString(); } public static string GetMonth(DateTime time) { int month = calendar.GetMonth(time); int year = calendar.GetYear(time); int leap = 0; //正月不可能闰月 for (int i = 3; i <= month; i++) { if (calendar.IsLeapMonth(year, i)) { leap = i; break; //一年中最多有一个闰月 } } if (leap > 0) month--; return (leap == month + 1 "闰" : "") + ChineseMonthName[month - 1]; } public static string GetDay(DateTime time) { return ChineseDayName[calendar.GetDayOfMonth(time) - 1]; } public static string GetStemBranch(DateTime time) { int sexagenaryYear = calendar.GetSexagenaryYear(time); string stemBranch = CelestialStem.Substring(sexagenaryYear % 10 - 1, 1) + TerrestrialBranch.Substring(sexagenaryYear % 12 - 1, 1); return stemBranch; } private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar(); private static string ChineseNumber = "〇一二三四五六七八九"; public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸"; public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥"; public static readonly string[] ChineseDayName = new string[] { "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十", "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十", "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"}; public static readonly string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; } 第二个类为自定义格式化器: using System; using System.Collections.Generic; using System.Text; using System.Globalization; using System.Threading; public class ChineseCalendarFormatter : IFormatProvider, ICustomFormatter { //实现IFormatProvider public object GetFormat(Type formatType) { if (formatType == typeof(ICustomFormatter)) return this; else return Thread.CurrentThread.CurrentCulture.GetFormat(formatType); } //实现ICustomFormatter public string Format(string format, object arg, IFormatProvider formatProvider) { string s; IFormattable formattable = arg as IFormattable; if (formattable == null) s = arg.ToString(); else s = formattable.ToString(format, formatProvider); if (arg.GetType() == typeof(DateTime)) { DateTime time = (DateTime)arg; switch (format) { case "D": //长日期格式 s = String.Format("{0}年{1}月{2}", ChineseCalendarHelper.GetYear(time), ChineseCalendarHelper.GetMonth(time), ChineseCalendarHelper.GetDay(time)); break; case "d": //短日期格式 s = String.Format("{0}年{1}月{2}", ChineseCalendarHelper.GetStemBranch(time), ChineseCalendarHelper.GetMonth(time), ChineseCalendarHelper.GetDay(time)); break; case "M": //月日格式 s = String.Format("{0}月{1}", ChineseCalendarHelper.GetMonth(time), ChineseCalendarHelper.GetDay(time)); break; case "Y": //年月格式 s = String.Format("{0}年{1}月", ChineseCalendarHelper.GetYear(time), ChineseCalendarHelper.GetMonth(time)); break; default: s = String.Format("{0}年{1}月{2}", ChineseCalendarHelper.GetYear(time), ChineseCalendarHelper.GetMonth(time), ChineseCalendarHelper.GetDay(time)); break; } } return s; } }
这段代码中间处理格式那部份稍做改进,就可以支持更多的日期格式。
有了这两段代码为原型,要实现计算和显示一个日期的农历日期及其它功能,基本上就很容易了。
<% 'Wonsoft, Welcome to visit my web http://wonsoft.cn '*********************************************** ' 类名称:ChinaDay ' 用途: ' 根据输入的日期计算该日期的农历天干地支及当年属相 ' 使用方法: ' 第一个参数为输入参数,不填写默认为当日, ' 只计算1921-2-8之后的日期 ' ##-------------------------------------------## ' Dim objChinaDay ' Dim sDay, sWeekDay, sChinaDay, sChinaYear,sChinaAni ' Set objChinaDay = New ChinaDay ' Call objChinaDay.Action("",sDay,sWeekDay,sChinaYear,sChinaDay,sChinaAni) ' Response.Write sDay&"<BR>" ' Response.Write sWeekDay&"<BR>" ' Response.Write sChinaYear&"<BR>" ' Response.Write sChinaDay&"<BR>" ' Response.Write sChinaAni&"<BR>" ' ##-------------------------------------------## ' Copyright: 本代码非原创,是2001年收集的,原作者未知。 ' License:Free '******************************************************* Class ChinaDay Dim arrWeekName(7), MonthAdd(11), NongliData(99) Dim arrTianGan(9), arrDiZhi(11), arrShuXiang(11), arrDayName(30), arrMonName(12) Dim curTime, curYear, curMonth, curDay, curWeekday Dim i, m, n, k, isEnd, bit, TheDate '初始化数据 Sub Class_Initialize() '--------------------------------------------------- '定义显示字串 '星期名 arrWeekName(0) = "*" arrWeekName(1) = "星期日" arrWeekName(2) = "星期一" arrWeekName(3) = "星期二" arrWeekName(4) = "星期三" arrWeekName(5) = "星期四" arrWeekName(6) = "星期五" arrWeekName(7) = "星期六" '天干名称 arrTianGan(0) = "甲" arrTianGan(1) = "乙" arrTianGan(2) = "丙" arrTianGan(3) = "丁" arrTianGan(4) = "戊" arrTianGan(5) = "己" arrTianGan(6) = "庚" arrTianGan(7) = "辛" arrTianGan(8) = "壬" arrTianGan(9) = "癸" '地支名称 arrDiZhi(0) = "子" arrDiZhi(1) = "丑" arrDiZhi(2) = "寅" arrDiZhi(3) = "卯" arrDiZhi(4) = "辰" arrDiZhi(5) = "巳" arrDiZhi(6) = "午" arrDiZhi(7) = "未" arrDiZhi(8) = "申" arrDiZhi(9) = "酉" arrDiZhi(10) = "戌" arrDiZhi(11) = "亥" '属相名称 arrShuXiang(0) = "鼠" arrShuXiang(1) = "牛" arrShuXiang(2) = "虎" arrShuXiang(3) = "兔" arrShuXiang(4) = "龙" arrShuXiang(5) = "蛇" arrShuXiang(6) = "马" arrShuXiang(7) = "羊" arrShuXiang(8) = "猴" arrShuXiang(9) = "鸡" arrShuXiang(10) = "狗" arrShuXiang(11) = "猪" '农历日期名 arrDayName(0) = "*" arrDayName(1) = "初一" arrDayName(2) = "初二" arrDayName(3) = "初三" arrDayName(4) = "初四" arrDayName(5) = "初五" arrDayName(6) = "初六" arrDayName(7) = "初七" arrDayName(8) = "初八" arrDayName(9) = "初九" arrDayName(10) = "初十" arrDayName(11) = "十一" arrDayName(12) = "十二" arrDayName(13) = "十三" arrDayName(14) = "十四" arrDayName(15) = "十五" arrDayName(16) = "十六" arrDayName(17) = "十七" arrDayName(18) = "十八" arrDayName(19) = "十九" arrDayName(20) = "二十" arrDayName(21) = "廿一" arrDayName(22) = "廿二" arrDayName(23) = "廿三" arrDayName(24) = "廿四" arrDayName(25) = "廿五" arrDayName(26) = "廿六" arrDayName(27) = "廿七" arrDayName(28) = "廿八" arrDayName(29) = "廿九" arrDayName(30) = "三十" '农历月份名 arrMonName(0) = "*" arrMonName(1) = "正" arrMonName(2) = "二" arrMonName(3) = "三" arrMonName(4) = "四" arrMonName(5) = "五" arrMonName(6) = "六" arrMonName(7) = "七" arrMonName(8) = "八" arrMonName(9) = "九" arrMonName(10) = "十" arrMonName(11) = "十一" arrMonName(12) = "腊" '--------------------------------------------------------- '公差数据定义 '公历每月前面的天数 MonthAdd(0) = 0 MonthAdd(1) = 31 MonthAdd(2) = 59 MonthAdd(3) = 90 MonthAdd(4) = 120 MonthAdd(5) = 151 MonthAdd(6) = 181 MonthAdd(7) = 212 MonthAdd(8) = 243 MonthAdd(9) = 273 MonthAdd(10) = 304 MonthAdd(11) = 334 '农历数据 NongliData(0) = 2635 NongliData(1) = 333387 NongliData(2) = 1701 NongliData(3) = 1748 NongliData(4) = 267701 NongliData(5) = 694 NongliData(6) = 2391 NongliData(7) = 133423 NongliData(8) = 1175 NongliData(9) = 396438 NongliData(10) = 3402 NongliData(11) = 3749 NongliData(12) = 331177 NongliData(13) = 1453 NongliData(14) = 694 NongliData(15) = 201326 NongliData(16) = 2350 NongliData(17) = 465197 NongliData(18) = 3221 NongliData(19) = 3402 NongliData(20) = 400202 NongliData(21) = 2901 NongliData(22) = 1386 NongliData(23) = 267611 NongliData(24) = 605 NongliData(25) = 2349 NongliData(26) = 137515 NongliData(27) = 2709 NongliData(28) = 464533 NongliData(29) = 1738 NongliData(30) = 2901 NongliData(31) = 330421 NongliData(32) = 1242 NongliData(33) = 2651 NongliData(34) = 199255 NongliData(35) = 1323 NongliData(36) = 529706 NongliData(37) = 3733 NongliData(38) = 1706 NongliData(39) = 398762 NongliData(40) = 2741 NongliData(41) = 1206 NongliData(42) = 267438 NongliData(43) = 2647 NongliData(44) = 1318 NongliData(45) = 204070 NongliData(46) = 3477 NongliData(47) = 461653 NongliData(48) = 1386 NongliData(49) = 2413 NongliData(50) = 330077 NongliData(51) = 1197 NongliData(52) = 2637 NongliData(53) = 268877 NongliData(54) = 3365 NongliData(55) = 531109 NongliData(56) = 2900 NongliData(57) = 2922 NongliData(58) = 398042 NongliData(59) = 2395 NongliData(60) = 1179 NongliData(61) = 267415 NongliData(62) = 2635 NongliData(63) = 661067 NongliData(64) = 1701 NongliData(65) = 1748 NongliData(66) = 398772 NongliData(67) = 2742 NongliData(68) = 2391 NongliData(69) = 330031 NongliData(70) = 1175 NongliData(71) = 1611 NongliData(72) = 200010 NongliData(73) = 3749 NongliData(74) = 527717 NongliData(75) = 1452 NongliData(76) = 2742 NongliData(77) = 332397 NongliData(78) = 2350 NongliData(79) = 3222 NongliData(80) = 268949 NongliData(81) = 3402 NongliData(82) = 3493 NongliData(83) = 133973 NongliData(84) = 1386 NongliData(85) = 464219 NongliData(86) = 605 NongliData(87) = 2349 NongliData(88) = 334123 NongliData(89) = 2709 NongliData(90) = 2890 NongliData(91) = 267946 NongliData(92) = 2773 NongliData(93) = 592565 NongliData(94) = 1210 NongliData(95) = 2651 NongliData(96) = 395863 NongliData(97) = 1323 NongliData(98) = 2707 NongliData(99) = 265877 End Sub '############################################################ '主要方法 Action ' inDay 输入日期,如果不输入则默认为当前日期 ' sDay 中文格式日期 ' sWeekDay 周几 ' sChinaYear 农历年 ' sChinaDay 农历日 ' sChinaAni 属相 '############################################################
Public Function Action(inDay,sDay,sWeekDay,sChinaYear,sChinaDay,sChinaAni) '转换要转换的日期 If inDay = "" Or Not IsDate(inDay) Then '获取当前系统时间 curTime = Now() Else curTime = CDate(inDay) End If If DateDiff("d",curTime,CDate("1921-2-8")) > 0 Then Exit Function End If '生成当前公历年、月、日 ==> sDay curYear = Year(curTime) curMonth = Month(curTime) curDay = Day(curTime) sDay = curYear & "年" If (curMonth < 10) Then sDaysDay = sDay & "0" & curMonth & "月" Else sDaysDay = sDay & curMonth & "月" End If If (curDay < 10) Then sDaysDay = sDay & "0" & curDay & "日" Else sDaysDay = sDay & curDay & "日" End If '生成当前公历星期 ==> sWeekDay curWeekday = Weekday(curTime) sWeekDay = arrWeekName(curWeekday) '计算到初始时间1921年2月8日的天数:1921-2-8(正月初一) TheDate = (curYear - 1921) * 365 + Int((curYear - 1921) / 4) + curDay + MonthAdd(curMonth - 1) - 38 If ((curYear Mod 4) = 0 And curMonth > 2) Then TheDateTheDate = TheDate + 1 End If '计算农历天干、地支、月、日 isEnd = 0 m = 0 '------------------------------------ Do If (NongliData(m) < 4095) Then k = 11 Else k = 12 End If n = k '------------------------------------ Do If (n < 0) Then Exit Do End If '获取NongliData(m)的第n个二进制位的值 bit = NongliData(m) For i = 1 To n Step 1 bit = Int(bit / 2) Next bitbit = bit Mod 2 If (TheDate <= 29 + bit) Then isEnd = 1 Exit Do End If TheDateTheDate = TheDate - 29 - bit nn = n - 1 Loop '------------------------------------ If (isEnd = 1) Then Exit Do End If mm = m + 1 Loop '------------------------------------ curYear = 1921 + m curMonth = k - n + 1 curDay = TheDate If (k = 12) Then If (curMonth = (Int(NongliData(m) / 65536) + 1)) Then curMonth = 1 - curMonth ElseIf (curMonth > (Int(NongliData(m) / 65536) + 1)) Then curMonthcurMonth = curMonth - 1 End If End If '生成农历天干、地支==> sChinaYear sChinaYear = "农历" & arrTianGan(((curYear - 4) Mod 60) Mod 10) & arrDiZhi(((curYear - 4) Mod 60) Mod 12) & "年" '生成属相 == > sChinaAni sChinaAni = arrShuXiang(((curYear - 4) Mod 60) Mod 12) '生成农历月、日 ==> NongliDayStr If (curMonth < 1) Then sChinaDay = "闰" & arrMonName( - 1 * curMonth) Else sChinaDay = arrMonName(curMonth) End If sChinaDaysChinaDay = sChinaDay & "月" sChinaDaysChinaDay = sChinaDay & arrDayName(curDay) End Function End Class
<!-- 中国农历开始 --> <SCRIPT language=JavaScript> <!-- var lunarInfo=new Array( 0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2, 0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977, 0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970, 0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950, 0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557, 0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0, 0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0, 0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6, 0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570, 0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0, 0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5, 0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930, 0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530, 0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45, 0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0) var Animals=new Array("鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"); var Gan=new Array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸"); var Zhi=new Array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"); var now = new Date(); var SY = now.getYear(); var SM = now.getMonth(); var SD = now.getDate(); //==== 传入 offset 传回干支, 0=甲子 function cyclical(num) { return(Gan[num%10]+Zhi[num%12])} //==== 传回农历 y年的总天数 function lYearDays(y) { var i, sum = 348 for(i=0x8000; i>0x8; i>>=1) sum += (lunarInfo[y-1900] & i) 1: 0 return(sum+leapDays(y)) } //==== 传回农历 y年闰月的天数 function leapDays(y) { if(leapMonth(y)) return((lunarInfo[y-1900] & 0x10000) 30: 29) else return(0) } //==== 传回农历 y年闰哪个月 1-12 , 没闰传回 0 function leapMonth(y) { return(lunarInfo[y-1900] & 0xf)} //====================================== 传回农历 y年m月的总天数 function monthDays(y,m) { return( (lunarInfo[y-1900] & (0x10000>>m)) 30: 29 )} //==== 算出农历, 传入日期物件, 传回农历日期物件 // 该物件属性有 .year .month .day .isLeap .yearCyl .dayCyl .monCyl function Lunar(objDate) { var i, leap=0, temp=0 var baseDate = new Date(1900,0,31) var offset = (objDate - baseDate)/86400000 this.dayCyl = offset + 40 this.monCyl = 14 for(i=1900; i<2050 && offset>0; i++) { temp = lYearDays(i) offset -= temp this.monCyl += 12 } if(offset<0) { offset += temp; i--; this.monCyl -= 12 } this.year = i this.yearCyl = i-1864 leap = leapMonth(i) //闰哪个月 this.isLeap = false for(i=1; i<13 && offset>0; i++) { //闰月 if(leap>0 && i==(leap+1) && this.isLeap==false) { --i; this.isLeap = true; temp = leapDays(this.year); } else { temp = monthDays(this.year, i); } //解除闰月 if(this.isLeap==true && i==(leap+1)) this.isLeap = false offset -= temp if(this.isLeap == false) this.monCyl ++ } if(offset==0 && leap>0 && i==leap+1) if(this.isLeap) { this.isLeap = false; } else { this.isLeap = true; --i; --this.monCyl;} if(offset<0){ offset += temp; --i; --this.monCyl; } this.month = i this.day = offset + 1 } function YYMMDD(){ var cl = '<font color="#0000df" STYLE="font-size:9pt;">'; if (now.getDay() == 0) cl = '<font color="#c00000" STYLE="font-size:9pt;">'; if (now.getDay() == 6) cl = '<font color="#00c000" STYLE="font-size:9pt;">'; return(cl+SY+'年'+(SM+1)+'月'+SD+'日</font>'); } function weekday(){ var day = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); var cl = '<font color="#ff0000" STYLE="font-size:9pt;">'; if (now.getDay() == 0) cl = '<font color="#c00000" STYLE="font-size:9pt;">'; if (now.getDay() == 6) cl = '<font color="#00c000" STYLE="font-size:9pt;">'; return(cl+ day[now.getDay()]+ '</font>'); } //==== 中文日期 function cDay(m,d){ var nStr1 = new Array('日','一','二','三','四','五','六','七','八','九','十'); var nStr2 = new Array('初','十','廿','卅',''); var s; if (m>10){s = '十'+nStr1[m-10]} else {s = nStr1[m]} s += '月' switch (d) { case 10:s += '初十'; break; case 20:s += '二十'; break; case 30:s += '三十'; break; default:s += nStr2[Math.floor(d/10)]; s += nStr1[d%10]; } return(s); } function solarDay1(){ var sDObj = new Date(SY,SM,SD); var lDObj = new Lunar(sDObj); var cl = '<font color="violet" STYLE="font-size:9pt;">'; var tt = '【'+Animals[(SY-4)%12]+'】'+cyclical(lDObj.monCyl)+'月 '+cyclical(lDObj.dayCyl++)+'日' ; return(cl+tt+'</font>'); } function solarDay2(){ var sDObj = new Date(SY,SM,SD); var lDObj = new Lunar(sDObj); var cl = '<font color="#000066" STYLE="font-size:9pt;">'; //农历BB'+(cld[d].isLeap '闰 ':' ')+cld[d].lMonth+' 月 '+cld[d].lDay+' 日 var tt = cyclical(SY-1900+36)+'年 '+cDay(lDObj.month,lDObj.day); return(cl+tt+'</font>'); } function solarDay3(){ var sTermInfo = new Array(0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758) var solarTerm = new Array("小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至") var lFtv = new Array("0101*春节","0115 元宵节","0505 端午节","0707 七夕情人节","0715 中元节","0815 中秋节","0909 重阳节","1208 腊八节","1224 小年","0100*除夕") var sFtv = new Array("0101*元旦","0214 情人节","0308 妇女节","0312 植树节","0315 消费者权益日", "0401 愚人节","0501 劳动节","0504 青年节","0512 护士节","0601 儿童节","0701 建党节 香港回归纪念", "0801 建军节","0808 父亲节","0908 茂生日","0909 毛泽东逝世纪念","0910 教师节","0928 孔子诞辰","1001*国庆节", "1006 老人节","1001 ListenWind生日","1024 联合国日","1112 孙中山诞辰","1220 澳门回归纪念","1225 圣诞节","1226 毛泽东诞辰") var sDObj = new Date(SY,SM,SD); var lDObj = new Lunar(sDObj); var lDPOS = new Array(3) var festival='',solarTerms='',solarFestival='',lunarFestival='',tmp1,tmp2; //农历节日 for(i in lFtv) if(lFtv[i].match(/^(/d{2})(.{2})([/s/*])(.+)$/)) { tmp1=Number(RegExp.$1)-lDObj.month tmp2=Number(RegExp.$2)-lDObj.day if(tmp1==0 && tmp2==0) lunarFestival=RegExp.$4 } //国历节日 for(i in sFtv) if(sFtv[i].match(/^(/d{2})(/d{2})([/s/*])(.+)$/)){ tmp1=Number(RegExp.$1)-(SM+1) tmp2=Number(RegExp.$2)-SD if(tmp1==0 && tmp2==0) solarFestival = RegExp.$4 } //节气 tmp1 = new Date((31556925974.7*(SY-1900)+sTermInfo[SM*2+1]*60000)+Date.UTC(1900,0,6,2,5)) tmp2 = tmp1.getUTCDate() if (tmp2==SD) solarTermsolarTerms = solarTerm[SM*2+1] tmp1 = new Date((31556925974.7*(SY-1900)+sTermInfo[SM*2]*60000)+Date.UTC(1900,0,6,2,5)) tmp2= tmp1.getUTCDate() if (tmp2==SD) solarTermsolarTerms = solarTerm[SM*2] if(solarTerms == '' && solarFestival == '' && lunarFestival == '') festival = ''; else festival = '<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#CCFFCC"><TR><TD>'+ '<FONT COLOR="#000000" STYLE="font-size:9pt;">'+solarTerms + ' ' + solarFestival + ' ' + lunarFestival+'</FONT></TD>'+ '</TR></TABLE>'; var cl = '<font color="#000066" STYLE="font-size:9pt;">'; return(cl+festival+'</font>'); } function setCalendar(){ document.write('<table align=center CELLPADDING=2 CELLSPACING=0 border=1><tr><td bgcolor=#FEFEEF><table border=0 CELLPADDING=0 CELLSPACING=0><tr><td align=center>'); document.write(YYMMDD()+' '+weekday()); document.write('</td></tr>'); document.write('<tr><td align=center>'); document.write(solarDay1()); document.write('</td></tr><tr><td align=center>'); document.write(solarDay2()); document.write('</td></tr><tr><td>'); document.write(solarDay3()); document.write('</td></tr></table></td></tr></table>'); } setCalendar(); //--> </SCRIPT> <!-- wonsoft(http://wonsoft.cn) --> <!-- 中国农历结束 -->
上面的代码不支持Firefox,请使用查找、替换,把getYear()修改为getFullYear()就可以支持firefox了。
农历FLASH源码、旧历FLASH日历、旧历FLASH时钟
在FLASH中,制作时钟、日历都不难。在网上搜索一下,也会有一大遍源码等着你去下载。但不知是我孤陋寡闻,还是咋地。在网上还没见过农历的代码,在民族文化如此繁荣的中国。竟然没有人来研究这个。实在是有点遗憾。花点时间,研究了一下。随后把源码附上。希望大家共同讨论。其中难的就是闰月部分。
制作过程,前面的就不说了。直接发代码吧。
新建一倒计时类"Taoshaw_data.as"。
然后输入如下代码:
class Taoshaw_data extends Date { //================版权声明开始================// //★淘沙网:http://www.taoshaw.com //★淘沙网所有教程欢迎转载。不过请转载时注明出处。谢谢合作。 //================版权声明结束================// /*============================================= 公元1900-2100两百年的农历类。 类的调用方法说明 getFullYear();获取新历年份 getnewMonth();获取月份 getDate()获取多少号. getSeconds();获取秒值 getDay();获取星期几 getHours();获取小时(整点) getMinutes();获取分数 getTaosJYear();获取中国农历年. getTaoJMonth(); 获取中国农历月. getTaoJDay();获取中国农历日. getTaoJNianZhu();获取年柱 getYueZhu();获取月柱 getRiZhu();获取日柱 getShiceng();获取时辰 getJieQi();获取二十四节气 =============================================*/
private var tao_txtnum_info = new Array(0x4bd8, 0x4ae0, 0xa570, 0x54d5, 0xd260, 0xd950, 0x5554, 0x56af, 0x9ad0, 0x55d2, 0x4ae0, 0xa5b6, 0xa4d0, 0xd250, 0xd255, 0xb54f, 0xd6a0, 0xada2, 0x95b0, 0x4977, 0x497f, 0xa4b0, 0xb4b5, 0x6a50, 0x6d40, 0xab54, 0x2b6f, 0x9570, 0x52f2, 0x4970, 0x6566, 0xd4a0, 0xea50, 0x6a95, 0x5adf, 0x2b60, 0x86e3, 0x92ef, 0xc8d7, 0xc95f, 0xd4a0, 0xd8a6, 0xb55f, 0x56a0, 0xa5b4, 0x25df, 0x92d0, 0xd2b2, 0xa950, 0xb557, 0x6ca0, 0xb550, 0x5355, 0x4daf, 0xa5b0, 0x4573, 0x52bf, 0xa9a8, 0xe950, 0x6aa0, 0xaea6, 0xab50, 0x4b60, 0xaae4, 0xa570, 0x5260, 0xf263, 0xd950, 0x5b57, 0x56a0, 0x96d0, 0x4dd5, 0x4ad0, 0xa4d0, 0xd4d4, 0xd250, 0xd558, 0xb540, 0xb6a0, 0x95a6, 0x95bf, 0x49b0, 0xa974, 0xa4b0, 0xb27a, 0x6a50, 0x6d40, 0xaf46, 0xab60, 0x9570, 0x4af5, 0x4970, 0x64b0, 0x74a3, 0xea50, 0x6b58, 0x5ac0, 0xab60, 0x96d5, 0x92e0, 0xc960, 0xd954, 0xd4a0, 0xda50, 0x7552, 0x56a0, 0xabb7, 0x25d0, 0x92d0, 0xcab5, 0xa950, 0xb4a0, 0xbaa4, 0xad50, 0x55d9, 0x4ba0, 0xa5b0, 0x5176, 0x52bf, 0xa930, 0x7954, 0x6aa0, 0xad50, 0x5b52, 0x4b60, 0xa6e6, 0xa4e0, 0xd260, 0xea65, 0xd530, 0x5aa0, 0x76a3, 0x96d0, 0x4afb, 0x4ad0, 0xa4d0, 0xd0b6, 0xd25f, 0xd520, 0xdd45, 0xb5a0, 0x56d0, 0x55b2, 0x49b0, 0xa577, 0xa4b0, 0xaa50, 0xb255, 0x6d2f, 0xada0, 0x4b63, 0x937f, 0x49f8, 0x4970, 0x64b0, 0x68a6, 0xea5f, 0x6b20, 0xa6c4, 0xaaef, 0x92e0, 0xd2e3, 0xc960, 0xd557, 0xd4a0, 0xda50, 0x5d55, 0x56a0, 0xa6d0, 0x55d4, 0x52d0, 0xa9b8, 0xa950, 0xb4a0, 0xb6a6, 0xad50, 0x55a0, 0xaba4, 0xa5b0, 0x52b0, 0xb273, 0x6930, 0x7337, 0x6aa0, 0xad50, 0x4b55, 0x4b6f, 0xa570, 0x54e4, 0xd260, 0xe968, 0xd520, 0xdaa0, 0x6aa6, 0x56df, 0x4ae0, 0xa9d4, 0xa4d0, 0xd150, 0xf252, 0xd520);
private var tao_txtsolarMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); //定义私有函数干支等信息 private var Gan = new Array("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"); private var Zhi = new Array("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"); private var Animals = new Array("鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"); //农历节气 private var tao_txtsolarTerm = new Array("小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白 "立冬", "小雪", "大雪", "冬至"); private var tao_txtsTermInfo = new Array(0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758); private var nStr1 = new Array('日', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'); private var nStr2 = new Array('初', '十', '廿', '卅', '□'); private var monthName = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"); private var cyear:Number; private var cmonth:Number; private var cday:Number; private var isLeap:Boolean; private var nianzhu:String; private var yuezhu:String; private var rizhu:String; private var jie:String; public function Taoshaw_data(yearOrTimevalue:Number, month:Number, date:Number, hour:Number, minute:Number, second:Number, millisecond:Number) { super(yearOrTimevalue, month, date, hour, minute, second, millisecond); var leap:Number = 0; var temp:Number = 0; var y:Number, m:Number, d; Number; y = getFullYear(); m = getMonth(); d = getDate(); var offset = (Date.UTC(y, m, d)-Date.UTC(1900, 0, 31))/86400000; for (var i = 1900; i<2100 && offset>0; i++) { temp = lYearDays(i); offset -= temp; } if (offset<0) { offset += temp; i--; } cyear = i; leap = leapMonth(i); isLeap = false; for (var i = 1; i<13 && offset>0; i++) { if (leap>0 && i == (leap+1) && isLeap == false) { --i; isLeap = true; temp = leapDays(cyear); } else { temp = monthDays(cyear, i); } if (isLeap == true && i == (leap+1)) { isLeap = false; } offset -= temp; } if (offset == 0 && leap>0 && i == leap+1) { if (isLeap) { isLeap = false; } else { isLeap = true; --i; } } if (offset<0) { offset += temp; --i; } cmonth = i; cday = offset+1; var cY, cM, cD; if (m<2) { cY = cyclical(y-1900+36-1); } else { cY = cyclical(y-1900+36); } cM = cyclical((y-1900)*12+m+12); var tmp1 = sTerm(y, m*2); var tmp2 = sTerm(y, m*2+1); if (d == tmp1) { jie = tao_txtsolarTerm[m*2]; } else { if (d == tmp2) { jie = tao_txtsolarTerm[m*2+1]; } else { jie = ""; } } this.jie = jie; //--------------------------------------------- var term2 = sTerm(y, 2); var firstNode = sTerm(y, m*2); if (m == 1 && d>=term2) { cY = cyclical(y-1900+36); trace("新年柱"+cY); } if ((d+1)>=firstNode) { cM = cyclical((y-1900)*12+m+13); } var dayCyclical = Date.UTC(y, m, 1, 0, 0, 0, 0)/86400000+25567+10; cD = cyclical(dayCyclical+d-1); nianzhu = cY; yuezhu = cM; rizhu = cD; } public function getTaosJYear():Number { return cyear; } public function getTaoJMonth():Number { return cmonth; } public function getTaoJDay():Number { return cday; } //getmonth() 得到的比当前月份小1.因为是以数组形式来存储月份的。下标是从0-11. public function getnewMonth():Number { return getMonth()+1; } public function getTaoJNianZhu():String { return nianzhu; } public function getYueZhu():String { return yuezhu; } public function getRiZhu():String { return rizhu; } public function getJieQi():String { return jie; } public function getShiceng():String { return Zhi[Math.round((getHours()%23)/2)]+"时"; } private function lYearDays(y) { var sum:Number = 348; for (var i = 0x8000; i>0x8; i >>= 1) { sum += (tao_txtnum_info[y-1900] & i) 1 : 0; } return (sum+leapDays(y)); } private function leapDays(y) { if (leapMonth(y)) { return ((tao_txtnum_info[y-1899] & 0xf) == 0xf 30 : 29); } else { return (0); } } private function leapMonth(y) { var lm = tao_txtnum_info[y-1900] & 0xf; return (lm == 0xf 0 : lm); } private function monthDays(y, m) { return ((tao_txtnum_info[y-1900] & (0x10000 >> m)) 30 : 29); } private function sTerm(y, n) { var offDate = new Date((31556925974.7*(y-1900)+tao_txtsTermInfo[n]*60000)+Date.UTC(1900, 0, 6, 2, 5)); return (offDate.getUTCDate()); } private function solarDays(y, m) { if (m == 1) { return (((y%4 == 0) && (y%100 != 0) || (y%400 == 0)) 29 : 28); } else { return (tao_txtsolarMonth[m]); } } private function cyclical(num) { return (Gan[num%10]+Zhi[num%12]); } }
保存。
2、用法上面也写得有。在FLASH中调用的时候。请参考如下代码:
/* getFullYear();获取新历年份 getnewMonth();获取月份,如果像本例这样,独立用数组表示,就直接用getMonth()获取,因为数组会少一个值.是从0开始的. getDate()获取多少号. getSeconds();获取秒值 getDay();获取星期几 getHours();获取小时(整点) getMinutes();获取分数 getTaosJYear();获取中国农历年. getTaoJMonth(); 获取中国农历月. getTaoJDay();获取中国农历日. getTaoJNianZhu();获取年柱 getYueZhu();获取月柱 getRiZhu();获取日柱 getShiceng();获取时辰 getJieQi();获取二十四节气 */ //定义新Taoshaw_data类,因为Taoshaw_data类继承于data类,所以在此直接使用此类即可. //需要实时更新时,就加onEnterFrame函数. onEnterFrame = function () { var my_data:Taoshaw_data = new Taoshaw_data(); //定义两个数组,用于显示**月,以及星期* var my_xingqi:Array = new Array(); my_xingqi = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; var my_yuefen:Array = new Array(); my_yuefen = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]; //定义变量myhaoshu获取当前日的号数 var myhaoshu = my_data.getDate(); //当数值小于10的时候,就在前面添加一个0; var miao = my_data.getSeconds(); var fen = my_data.getMinutes(); if (fen<10) { fen = "0"+fen; } else { } if (miao<10) { miao = "0"+miao; } else { } if (myhaoshu<10) { myhaoshu = "0"+myhaoshu; } else { }
show_txt.text = "现在是"+my_data.getFullYear()+"年"+my_yuefen[my_data.getMonth()]+myhaoshu+"号"+my_xingqi[my_data.getDay()]+my_data.getHours()+"时"+fen+"分"+miao+"秒。"+"旧历"+my_data.getTaosJYear()+"."+my_data.getTaoJMonth()+"."+my_data.getTaoJDay()+"农历"+my_data.getTaoJNianZhu()+"年"+my_data.getYueZhu()+"月"+my_data.getRiZhu()+"日"+my_data.getShiceng()+my_data.getJieQi();
};源码下载:http://download.csdn.net/source/830951
/*------------农历转换函数-----------*/ char *GetDayOf(PSYSTEMTIME pSt) { /*天干名称*/ const char *cTianGan[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"}; /*地支名称*/ const char *cDiZhi[] = {"子","丑","寅","卯","辰","巳","午", "未","申","酉","戌","亥"}; /*属相名称*/ const char *cShuXiang[] = {"鼠","牛","虎","兔","龙","蛇", "马","羊","猴","鸡","狗","猪"}; /*农历日期名*/ const char *cDayName[] = {"*","初一","初二","初三","初四","初五", "初六","初七","初八","初九","初十", "十一","十二","十三","十四","十五", "十六","十七","十八","十九","二十", "廿一","廿二","廿三","廿四","廿五", "廿六","廿七","廿八","廿九","三十"}; /*农历月份名*/ const char *cMonName[] = {"*","正","二","三","四","五","六", "七","八","九","十","十一","腊"}; /*公历每月前面的天数*/ const int wMonthAdd[12] = {0,31,59,90,120,151,181,212,243,273,304,334}; /*农历数据*/ const int wNongliData[100] = {2635,333387,1701,1748,267701,694,2391,133423,1175,396438 ,3402,3749,331177,1453,694,201326,2350,465197,3221,3402 ,400202,2901,1386,267611,605,2349,137515,2709,464533,1738 ,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762 ,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413 ,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395 ,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031 ,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222 ,268949,3402,3493,133973,1386,464219,605,2349,334123,2709 ,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877}; static int wCurYear,wCurMonth,wCurDay; static int nTheDate,nIsEnd,m,k,n,i,nBit; TCHAR szNongli[30], szNongliDay[10],szShuXiang[10]; /*---取当前公历年、月、日---*/ wCurYear = pSt->wYear; wCurMonth = pSt->wMonth; wCurDay = pSt->wDay; /*---计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)---*/ nTheDate = (wCurYear - 1921) * 365 + (wCurYear - 1921) / 4 + wCurDay + wMonthAdd[wCurMonth - 1] - 38; if((!(wCurYear % 4)) && (wCurMonth > 2)) nTheDate = nTheDate + 1; /*--计算农历天干、地支、月、日---*/ nIsEnd = 0; m = 0; while(nIsEnd != 1) { if(wNongliData[m] < 4095) k = 11; else k = 12; n = k; while(n>=0) { //获取wNongliData(m)的第n个二进制位的值 nBit = wNongliData[m]; for(i=1;i<n+1;i++) nBit = nBit/2; nBit = nBit % 2; if (nTheDate <= (29 + nBit)) { nIsEnd = 1; break; } nTheDate = nTheDate - 29 - nBit; n = n - 1; } if(nIsEnd) break; m = m + 1; } wCurYear = 1921 + m; wCurMonth = k - n + 1; wCurDay = nTheDate; if (k == 12) { if (wCurMonth == wNongliData[m] / 65536 + 1) wCurMonth = 1 - wCurMonth; else if (wCurMonth > wNongliData[m] / 65536 + 1) wCurMonth = wCurMonth - 1; } /*--生成农历天干、地支、属相 ==> wNongli--*/ wsprintf(szShuXiang,"%s",cShuXiang[((wCurYear - 4) % 60) % 12]); wsprintf(szNongli,"%s(%s%s)年",szShuXiang,cTianGan[((wCurYear - 4) % 60) % 10],cDiZhi[((wCurYear - 4) % 60) % 12]); /*--生成农历月、日 ==> wNongliDay--*/ if (wCurMonth < 1) wsprintf(szNongliDay,"闰%s",cMonName[-1 * wCurMonth]); else strcpy(szNongliDay,cMonName[wCurMonth]); strcat(szNongliDay,"月"); strcat(szNongliDay,cDayName[wCurDay]); return strcat(szNongli,szNongliDay); }
/* prototype: int calconv( struct convdate * ); struct convdate { int source; ==0 则输入日期为西历, !=0 则输入为农历 int solaryear; 输出或输入之西历年份 int solarmonth; 西历月 int solardate; 西历日 int lunaryear; 输出或输入之农历年份 int lunarmonth; 农历月 int lunardate; 农历日 int weekday; 该日为星期几 ( 0==星期日, 1==星期一, ... ) int kan; 该日天干 ( 0==甲, 1==乙, ..., 9==癸 ) int chih; 该日地支 ( 0==子, 1==丑, ..., 11==亥 ) };
呼叫时须设定 souce 的值, 若为 0 则为西历转农历, 否则为农历转西历. 然後视
输入为西历或农历来设定西历或农历的年月日. 转换後的年月日会填入结构中( 农
历或西历 ), 以及该日为星期几, 天干地支.
若函式的返回值为 0 表示没有错误, 1 为输入之年份错误, 2 为输入之月份错误,
3 为输入之日期错误.
输入之西历年须在 1937 - 2031 间
输入之农历年须在 1936 - 2030 间
若须扩充, 则增加 lunarcal[]
*/
#define firstyear 1936 /* the first year in lunarcal[] */ struct convdate { int source; int solaryear; int solarmonth; int solardate; int lunaryear; int lunarmonth; int lunardate; int weekday; int kan; int chih; }; struct taglunarcal { int basedays; /* 到西历 1 月 1 日到农历正月初一的累积日数 */ int intercalation; /* 闰月月份. 0==此年没有闰月 */ int baseweekday; /* 此年西历 1 月 1 日为星期几再减 1 */ int basekanchih; /* 此年西历 1 月 1 日之干支序号减 1 */ int monthdays[13]; /* 此农历年每月之大小, 0==小月(29日), 1==大月(30日)*/ }; struct taglunarcal lunarcal[] = { { 23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 }, /* 1936 */ { 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 }, { 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 }, { 49, 0, 6, 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, 0, 1, 0, 1, 0, 0 }, { 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 } }; #define lastyear (firstyear+sizeof(lunarcal)/sizeof(struct taglunarcal)-1) /* 西历年每月之日数 */ int solarcal[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* 西历年每月之累积日数, 平年与闰年 */ int solardays[2][14] = { { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396 }, { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 } }; /* 求此西历年是否为闰年, 返回 0 为平年, 1 为闰年 */ int getleap( int year ) { if ( year % 400 == 0 ) return 1; else if ( year % 100 == 0 ) return 0; else if ( year % 4 == 0 ) return 1; else return 0; } /* 西历农历转换 */ int calconv( struct convdate *cd ) { int leap, d, sm, y, im, l1, l2, acc, i, lm, kc; if ( cd->source == 0 ) /* solar */ { if ( cd->solaryear <= firstyear || cd->solaryear > lastyear ) return 1; sm = cd->solarmonth - 1; if ( sm < 0 || sm > 11 ) return 2; leap = getleap( cd->solaryear ); if ( sm == 1 ) d = leap + 28; else d = solarcal[sm]; if ( cd->solardate < 1 || cd->solardate > d ) return 3; y = cd->solaryear - firstyear; acc = solardays[leap][sm] + cd->solardate; cd->weekday = ( acc + lunarcal[y].baseweekday ) % 7; kc = acc + lunarcal[y].basekanchih; cd->kan = kc % 10; cd->chih = kc % 12; if ( acc <= lunarcal[y].basedays ) { y--; cd->lunaryear = cd->solaryear - 1; leap = getleap( cd->lunaryear ); sm += 12; acc = solardays[leap][sm] + cd->solardate; } else cd->lunaryear = cd->solaryear; l1 = lunarcal[y].basedays; for ( i=0; i<13; i++ ) { l2 = l1 + lunarcal[y].monthdays[i] + 29; if ( acc <= l2 ) break; l1 = l2; } cd->lunarmonth = i + 1; cd->lunardate = acc - l1; im = lunarcal[y].intercalation; if ( im != 0 && cd->lunarmonth > im ) { cd->lunarmonth--; if ( cd->lunarmonth == im ) cd->lunarmonth = -im; } if ( cd->lunarmonth > 12 ) cd->lunarmonth -= 12; } else /* lunar */ { if ( cd->lunaryear < firstyear || cd->lunaryear >= lastyear ) return 1; y = cd->lunaryear - firstyear; im = lunarcal[y].intercalation; lm = cd->lunarmonth; if ( lm < 0 ) { if ( lm != -im ) return 2; } else if ( lm < 1 || lm > 12 ) return 2; if ( im != 0 ) { if ( lm > im ) lm++; else if ( lm == -im ) lm = im + 1; } lm--; if ( cd->lunardate > lunarcal[y].monthdays[lm] + 29 ) return 3; acc = lunarcal[y].basedays; for ( i=0; i acc += lunarcal[y].monthdays[i] + 29; acc += cd->lunardate; leap = getleap( cd->lunaryear ); for ( i=13; i>=0; i-- ) if ( acc > solardays[leap][i] ) break; cd->solardate = acc - solardays[leap][i]; if ( i <= 11 ) { cd->solaryear = cd->lunaryear; cd->solarmonth = i + 1; } else { cd->solaryear = cd->lunaryear + 1; cd->solarmonth = i - 11; } leap = getleap( cd->solaryear ); y = cd->solaryear - firstyear; acc = solardays[leap][cd->solarmonth-1] + cd->solardate; cd->weekday = ( acc + lunarcal[y].baseweekday ) % 7; kc = acc + lunarcal[y].basekanchih; cd->kan = kc % 10; cd->chih = kc % 12; } return 0; }
(感谢各位作者)
文献:
1、http://www.25175.com/200609/25175/25175_html/2006-11/936.html
2、http://hi.baidu.com/wangleiit/blog/item/d2ef51088122bf910b7b827c.html
3、http://www.supesoft.com/ArticleDisp.asp ID=4618
4、http://www.cnblogs.com/panjiwen/archive/2006/02/06/326159.html
5、http://www.taoshaw.com/taoshaw/article.asp id=1494
6、http://www.pcjx.com/Dotnet/C/208963.html
7、http://study.feloo.com/gaokao_news.do infoID=50354
本页共1456段,51264个字符,61310 Byte(字节)