- 精品下载 | 实用查询 | 词典查询 | 桌面壁纸 | 网址 | 笑话 | FLASH频道 | 天气文章资讯 | 站长工具 | 证件办理 | 闪字生成 | 广告代码 | 在线手册 | 有问必答
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> .NET >> c#(Csharp) >> 正文
站内文章搜索:           

一个阴历阳历互相转化的类(c#农历)

作者:佚名    文章来源:本站原创    更新时间 :2007-6-4 22:10:53
x87, 0x87, //2039
    0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, //2040
    0xA5, 0xC3, 0xA5, 0xB5, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, //2041
    0xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2042
    0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2043
    0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x88, 0x87, 0x96, //2044
    0xA5, 0xC3, 0xA5, 0xB4, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, //2045
    0xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x87, //2046
    0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2047
    0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA5, 0x97, 0x87, 0x87, 0x88, 0x86, 0x96, //2048
    0xA4, 0xC3, 0xA5, 0xA5, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x86, //2049
    0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x78, 0x78, 0x87, 0x87}; //2050
  
  
  private DateTime m_Date;
  public DateTime Date
  {
   get{ return m_Date;}
   set{ m_Date = value;}
  }
  
  public CNDate()
  {
   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 "";};
  }
  
  //计算公历当天对应的节气 0-23,-1表示不是节气
  public int l_GetLunarHolDay()
  {
   byte Flag;
   int Day, iYear, iMonth, iDay;
   iYear = m_Date.Year;
   if ((iYear < START_YEAR) || (iYear > END_YEAR))
   { return -1;};
   iMonth = m_Date.Month;
   iDay = m_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 = "";
    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 = szText1.Substring((iDay / 10) , 1);
    strDay = strDay + "十";
   }
   return strDay;
  }
  
  public string GetLunarHolDay()
  {
   ushort iYear, iMonth, iDay;
   int i;
   TimeSpan ts;
   iYear = (ushort)(m_Date.Year);
   if ((iYear < START_YEAR) || (iYear > END_YEAR))
   { return "";};
   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));
    return FormatMonth(iMonth, true) + FormatLunarDay(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)&0x00FF; //取低位
   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)&0x00FF; //取低位
   }
   //计算日
   iDay = (ushort)(iDay + iSpanDays);
   iDay=iDay;
  }
  
  
  //把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

 public class Test
 {
  static void Main(string[] args)
  {
   for(int m=0;m<70;m++)
   {
    DateTime date1=Convert.ToDateTime("2020-01-01");               
    CNDate dt = new CNDate(date1.AddDays(m));
    Console.WriteLine("---:"+date1.AddDays(m));
    if(dt.GetLunarHolDay()=="正月初一")
    {
     Console.WriteLine("年的春节是:"+dt.GetLunarHolDay());
     Console.WriteLine("年的春节是:"+date1.AddDays(m));
     break;
    }

   }
//
//   CNDate dt = new CNDate(DateTime.Today);
//            DateTime date=DateTime.Today;
//   Console.WriteLine("今天是:" + dt.Date.ToString() + dt.GetConstellationName());
//   Console.WriteLine(dt.l_GetLunarHolDay());
//   Console.WriteLine(dt.GetLunarHolDay());
//
//   Console.WriteLine("闰月" + dt.GetLeapMonth(UInt16.Parse(date.Year.ToString())));
//   Console.WriteLine("2月的天数" + dt.LunarMonthDays(UInt16.Parse(date.Year.ToString()), UInt16.Parse(date.Month.ToString())));
//   Console.WriteLine("天数" + dt.LunarYearDays(UInt16.Parse(date.Year.ToString())));
//   Console.WriteLine("" + dt.FormatLunarYear());
  }
 } //class Test

}

 

上一页  [1] [2] 

 
【相关文章:】
没有相关文章

发表评论】【打印此文】【关闭窗口】【点击数:
★好玩的休闲小游戏★
网友评论:
数据载入中,请稍后……