
除了天数差计算,基本上是完成了你所提出的功能(天数差计算其实已经提供了年内序数计算方方法,可以在此基础上完善即可)。包含星期计算、公历转农历、农历转公历、节气查询。数据与算法均为原创,绝非网上复制而来。附件中有源代码,同时附带农历数据提取工具,可提取1600到7000年之间的任意年份区间,提取后的数据为压缩数据,占用空间小,同时数据包含数九、三伏、入梅和出梅,非常适合写一份完整的万年历。
部分核心算法函数如下:
//判断闰年,参数:年份,返回值:0-平年,1-闰年int IsLeapYear(int iYear)
//计算日期在年内的序数,参数:年,月,日,年内序数,返回值:0-失败,1-成功
int GetDaysNumInYear(int iYear, int iMonth, int iDay,int *nDays)
//从年内序数计算月、日,参数:年,年内序数,月,日,返回值:0-失败,1-成功
int GetDateFromDays(int iYear, int nDays, int *iMonth, int *iDay)
//检验年、月、日的合法性,参数:年,月,日,返回值:0-失败,1-成功
int DateCheck(int iYear,int iMonth,int iDay)
//获取农历新年的公历年内序数,参数:农历年,返回值:农历新年的公历年内序数
int LunarGetNewYearOrdinal(int iLunarYear)
//获取农历月的天数,参数:农历年,农历月,是否为闰月,返回值:该农历月的天数,为0代表参数无效
int LunarGetDaysofMonth(int iLunarYear,int iLunarMonth,int IsLeapMonth)
//展开大小月数据表(某一年的),参数:农历年,从上一年十一月开始到当前年份(闰)十二月的每月天数,返回值:0-失败,1-成功
int LunarExpandDX(int iLunarYear,int iDayOfMonth[15])
//获取农历某一年的闰月情况,参数:农历年,返回值,该年的闰月月份,0表示无闰月
int LunarGetLeapMonth(int iLunarYear)
//公历转农历,参数:公历年、月、日,农历年、月、日,是否为闰月,返回值:0-失败,1-成功
int Gongli2Nongli(int iYear,int iMonth,int iDay,int *iLunarYear,int *iLunarMonth,int *iLunarDay,int *iIsLeapMonth)
//农历转公历,参数:家历年、月、日,是否为闰月,公历年、月、日,返回值:0-失败,1-成功
int Nongli2Gongli(int iLunarYear,int iLunarMonth,int iLunarDay,int iIsLeapMonth,int *iYear,int *iMonth,int *iDay)
//得到指定年份的节气信息,首个是小寒
int GetJieQi(int iYear,int uMonth,unsigned int uJieQi[2])
//计算星期
unsigned int GetDayOfWeek(int iYear,int uMonth,int uDay)
源代码请看
吧。提取码:ooj7用VB做万年历,非常关键点就是农历写法,参考代码如下:#Region " 返回农历 "
'返回农历
'cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28
'cCalendar.MinSupportedDateTime 返回支持的最小日期,即190-2-19
Private cCalendar As New System.Globalization.ChineseLunisolarCalendar
Public Function PubFunGet_CNDate(ByVal sDateTime As Date) As String
cCalendar = New System.Globalization.ChineseLunisolarCalendar
Dim lyear As Integer = cCalendar.GetYear(sDateTime)
Dim lmonth As Integer = cCalendar.GetMonth(sDateTime)
Dim lday As Integer = cCalendar.GetDayOfMonth(sDateTime)
Dim lweek As Integer = cCalendar.GetDayOfWeek(sDateTime)
'获取闰月, 0 则表示没有闰月
Dim leapMonth As Integer = cCalendar.GetLeapMonth(lyear)
Dim isleap As Boolean = False
If (leapMonth >0) Then
If (leapMonth = lmonth) Then
'闰月
isleap = True
lmonth = lmonth - 1
ElseIf (lmonth >leapMonth) Then
lmonth = lmonth - 1
End If
End If
Return String.Concat(GetLunisolarYear(lyear), IIf(isleap = True, "闰年", "年"), GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday))
End Function
'十天干
Private tiangan As String() = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}
'十二地支
Private dizhi As String() = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}
'十二生肖
Private shengxiao As String() = {"鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"}
'农历月
Private months As String() = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)"}
'农历日
Private days1 As String() = {"初", "十", "廿", "三"}
Private days As String() = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"}
'返回农历年(天干 地支 生肖)
Private Function GetLunisolarYear(ByVal year As Integer) As String
GetLunisolarYear = ""
If (year >3) Then
Dim tgIndex As Integer = (year - 4) Mod 10
Dim dzIndex As Integer = (year - 4) Mod 12
Return tiangan(tgIndex) &dizhi(dzIndex) &"[" &shengxiao(dzIndex) &"]"
End If
'无效的年份!
End Function
'返回生肖
Private Function GetShengXiao(ByVal sDateTime As Date) As String
Return shengxiao(cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(sDateTime)) - 1)
End Function
'返回农历月
Private Function GetLunisolarMonth(ByVal month As Integer) As String
GetLunisolarMonth = ""
If (month <13 AndAlso month >0) Then
Return months(month - 1)
End If
'无效的月份!
End Function
'返回农历日
Private Function GetLunisolarDay(ByVal day As Integer) As String
GetLunisolarDay = ""
If (day >0 AndAlso day <32) Then
If (day <>20 AndAlso day <>30) Then
Return String.Concat(days1((day - 1) \ 10), days((day - 1) Mod 10))
Else
Return String.Concat(days((day - 1) \ 10), days1(1))
End If
End If
'无效的日!
End Function
#End Region
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)