📄 commonclass.cs
字号:
{
Date = DateTime.Today;
}
public CNDate(DateTime dt)
{
Date = dt.Date;
}
//计算指定日期的星座序号
public int GetConstellation()
{
int Y, M, D;
Y = m_Date.Year;
M = m_Date.Month;
D = m_Date.Day;
Y = M * 100 + D;
if (((Y >= 321) && (Y <= 419))) { return 0;}
else if ((Y >= 420) && (Y <= 520)) { return 1;}
else if ((Y >= 521) && (Y <= 620)) { return 2;}
else if ((Y >= 621) && (Y <= 722)) { return 3;}
else if ((Y >= 723) && (Y <= 822)) { return 4;}
else if ((Y >= 823) && (Y <= 922)) { return 5;}
else if ((Y >= 923) && (Y <= 1022)) { return 6;}
else if ((Y >= 1023) && (Y <= 1121)) { return 7;}
else if ((Y >= 1122) && (Y <= 1221)) { return 8;}
else if ((Y >= 1222) || (Y <= 119)) { return 9;}
else if ((Y >= 120) && (Y <= 218)) { return 10;}
else if ((Y >= 219) && (Y <= 320)) { return 11;}
else { return -1;};
}
//计算指定日期的星座名称
public string GetConstellationName()
{
int Constellation;
Constellation = GetConstellation();
if ((Constellation >= 0) && (Constellation <= 11))
{
return ConstellationName[Constellation];
}
else
{
return string.Empty;
};
}
//计算公历当天对应的节气 0-23,-1表示不是节气
public int l_GetLunarHolDay()
{
DateTime date = m_Date;
byte Flag;
int Day, iYear, iMonth, iDay;
iYear = date.Year;
if ((iYear < START_YEAR) || (iYear > END_YEAR)) return -1;
iMonth = date.Month;
iDay = date.Day;
Flag = gLunarHolDay[(iYear - START_YEAR) * 12 + iMonth - 1];
if (iDay < 15)
Day = 15 - ((Flag >> 4) & 0x0f);
else
Day = (Flag & 0x0f) + 15;
if (iDay == Day)
{
if (iDay > 15)
return (iMonth - 1) * 2 + 1;
else
return (iMonth - 1) * 2;
}
else
{
return -1;
};
}
public string FormatMonth(ushort iMonth, bool bLunar)
{
string szText = "正二三四五六七八九十";
string strMonth;
if ((!bLunar) && (iMonth == 1))
{
return "一月";
}
if (iMonth <= 10)
{
strMonth = string.Empty;
strMonth = strMonth + szText.Substring(iMonth - 1, 1);
strMonth = strMonth + "月";
return strMonth;
}
if (iMonth == 11)
{
strMonth = "十一";
}
else
{
strMonth = "十二";
}
return strMonth + "月";
}
public string FormatLunarDay(ushort iDay)
{
string szText1 = "初十廿三";
string szText2 = "一二三四五六七八九十";
string strDay;
if ((iDay != 20) && (iDay != 30))
{
strDay = szText1.Substring((iDay - 1) / 10, 1);
strDay = strDay + szText2.Substring((iDay - 1) % 10, 1);
}
else
{
strDay = szText1.Substring((iDay / 10) * 2 + 1, 2);
strDay = strDay + "十";
}
return strDay;
}
public string GetLunarHolDay(bool includeYear)
{
ushort iYear, iMonth, iDay;
int i;
TimeSpan ts;
iYear = (ushort)(m_Date.Year);
if ((iYear < START_YEAR) || (iYear > END_YEAR))
return string.Empty;
i = l_GetLunarHolDay();
if ((i >= 0) && (i <= 23))
return LunarHolDayName[i];
else
{
ts = m_Date - (new DateTime(START_YEAR, 1, 1));
l_CalcLunarDate(out iYear, out iMonth, out iDay, (uint)(ts.Days));
string result = FormatMonth(iMonth, true) + FormatLunarDay(iDay);
if (includeYear) result = iYear.ToString() + "年" + result;
return result;
}
}
public DateTime GetLunarHolDate()
{
return GetLunarHolDate(m_Date);
}
public DateTime GetLunarHolDate(DateTime inDate)
{
DateTime date = inDate;
ushort iYear, iMonth, iDay;
TimeSpan ts;
iYear = (ushort)(date.Year);
ts = m_Date - (new DateTime(START_YEAR, 1, 1));
l_CalcLunarDate(out iYear, out iMonth, out iDay, (uint)(ts.Days));
if (0 != (iDay) % 10)
iDay = (ushort)((iDay) % 10);
return new DateTime(iYear, iMonth, iDay);
}
//返回阴历iLunarYear年的闰月月份,如没有返回0 1901年1月---2050年12月
public int GetLeapMonth(ushort iLunarYear)
{
byte Flag;
if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR))
return 0;
Flag = gLunarMonth[(iLunarYear - START_YEAR) / 2];
if ((iLunarYear - START_YEAR) % 2 == 0)
return Flag >> 4;
else
return Flag & 0x0F;
}
//返回阴历iLunarYer年阴历iLunarMonth月的天数,如果iLunarMonth为闰月,
//高字为第二个iLunarMonth月的天数,否则高字为0 1901年1月---2050年12月
public uint LunarMonthDays(ushort iLunarYear, ushort iLunarMonth)
{
int Height, Low;
int iBit;
if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR))
return 30;
Height = 0;
Low = 29;
iBit = 16 - iLunarMonth;
if ((iLunarMonth > GetLeapMonth(iLunarYear)) && (GetLeapMonth(iLunarYear) > 0))
iBit--;
if ((gLunarMonthDay[iLunarYear - START_YEAR] & (1 << iBit)) > 0)
Low++;
if (iLunarMonth == GetLeapMonth(iLunarYear))
{
if ((gLunarMonthDay[iLunarYear - START_YEAR] & (1 << (iBit-1)))>0)
Height = 30;
else
Height = 29;
}
return (uint)((uint)(Low)|(uint)(Height)<<16); //合成为uint
}
//返回阴历iLunarYear年的总天数 1901年1月---2050年12月
public int LunarYearDays(ushort iLunarYear)
{
int Days;
uint tmp;
if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR))
return 0;
Days = 0;
for (ushort i=1; i <= 12; i++)
{
tmp = LunarMonthDays(iLunarYear, i);
Days = Days + ((ushort)(tmp>>16) & 0xFFFF); //取高位
Days = Days + (ushort)(tmp); //取低位
}
return Days;
}
//计算从1901年1月1日过iSpanDays天后的阴历日期
public void l_CalcLunarDate(out ushort iYear, out ushort iMonth, out ushort iDay, uint iSpanDays)
{
uint tmp;
//阳历1901年2月19日为阴历1901年正月初一
//阳历1901年1月1日到2月19日共有49天
if (iSpanDays < 49)
{
iYear = START_YEAR - 1;
if (iSpanDays < 19)
{
iMonth = 11;
iDay = (ushort)(11 + iSpanDays);
}
else
{
iMonth = 12;
iDay = (ushort)(iSpanDays - 18);
}
return;
}
//下面从阴历1901年正月初一算起
iSpanDays = iSpanDays - 49;
iYear = START_YEAR;
iMonth = 1;
iDay = 1;
//计算年
tmp = (uint)LunarYearDays(iYear);
while (iSpanDays >= tmp)
{
iSpanDays = iSpanDays - tmp;
iYear++;
tmp = (uint)LunarYearDays(iYear);
}
//计算月
tmp = LunarMonthDays(iYear, iMonth); //取低位
while (iSpanDays >= tmp)
{
iSpanDays = iSpanDays - tmp;
if (iMonth == GetLeapMonth(iYear))
{
tmp = (LunarMonthDays(iYear, iMonth)>>16)&0xFFFF; //取高位
if (iSpanDays < tmp) break;
iSpanDays = iSpanDays - tmp;
}
iMonth++;
tmp = LunarMonthDays(iYear,iMonth); //取低位
}
//计算日
iDay = (ushort)(iDay + iSpanDays);
}
//把iYear年格式化成天干记年法表示的字符串
public string FormatLunarYear()
{
string strYear;
string szText1 = "甲乙丙丁戊己庚辛壬癸";
string szText2 = "子丑寅卯辰巳午未申酉戌亥";
string szText3 = "鼠牛虎免龙蛇马羊猴鸡狗猪";
ushort iYear;
iYear = (ushort)(m_Date.Year);
strYear = szText1.Substring((iYear - 4) % 10, 1);
strYear = strYear + szText2.Substring((iYear - 4) % 12, 1);
strYear = strYear + " ";
strYear = strYear + szText3.Substring((iYear - 4) % 12, 1);
strYear = strYear + "年";
return strYear;
}
} //class CNDate
#endregion
/*public class CNDateTest
{
public static void TestCNDate()
{
CNDate dt = new CNDate(DateTime.Today);
Console.WriteLine("今天是:" + dt.Date.ToString() + dt.GetConstellationName());
Console.WriteLine(dt.l_GetLunarHolDay());
Console.WriteLine(dt.GetLunarHolDay(false));
//Console.WriteLine("闰月" + dt.GetLeapMonth(UInt16.Parse(args[0])));
//Console.WriteLine("2月的天数" + dt.LunarMonthDays(UInt16.Parse(args[0]), UInt16.Parse(args[1])));
//Console.WriteLine("天数" + dt.LunarYearDays(UInt16.Parse(args[0])));
Console.WriteLine("" + dt.FormatLunarYear());
dt.Date = DateTime.Today.AddDays(1);
Console.WriteLine("明天是:" + dt.Date.ToString() + dt.GetConstellationName());
}
} //class Test
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -