/// <summary> /// ay 2015年11月19日17:31:25 /// 增加 /// </summary> /// <param name="Cyear"></param> /// <param name="Cmonth"></param> /// <param name="Cday"></param> /// <returns></returns> public static DayInfo GetNongLi(int Cyear, int Cmonth, int Cday) { DateTime dt = new DateTime(Cyear, Cmonth, Cday); DayInfo result = new DayInfo(); result.Day = GetDay(dt); result.TitleType = 0; result.Title = result.Day; result.Month = GetMonth(dt); result.MonthAndDay = result.Month + result.Day; if (result.Day == "初一") { result.Title = result.Month; result.TitleType = 1; } //获得节气,节日,中国农历节日 string strJQ = GetSolarTerm(dt); if (strJQ != "") { result.JieQi = strJQ; result.Title = result.JieQi; result.TitleType = 2; } string strHoliday = GetHoliday(dt); if (strHoliday != "") { result.GongHoliday = strHoliday; result.Title = result.GongHoliday; result.TitleType = 3; } string strChinaHoliday = GetChinaHoliday(dt); if (strChinaHoliday != "") { result.NongHoliday = strChinaHoliday; result.Title = result.NongHoliday; result.TitleType = 4; } int yearIndex = china.GetSexagenaryYear(dt); string yearTG = " 甲乙丙丁戊己庚辛壬癸"; string yearDZ = " 子丑寅卯辰巳午未申酉戌亥"; string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪"; int year = china.GetYear(dt); int yTG = china.GetCelestialStem(yearIndex); int yDZ = china.GetTerrestrialBranch(yearIndex); result.Year = year; result.TianGan = yearTG[yTG].ToString(); result.DiGan = yearDZ[yDZ].ToString(); result.ShengXiao = yearSX[yDZ].ToString(); return(result); }
//年采用的头尾法,月采用的是节令法,主流日历基本上都这种结合,如百度的日历 private string GetEraYear() { var sexagenaryYear = _chineseDateTime.GetSexagenaryYear(_dateTime); var stemIndex = _chineseDateTime.GetCelestialStem(sexagenaryYear) - 1; var branchIndex = _chineseDateTime.GetTerrestrialBranch(sexagenaryYear) - 1; return($"{_celestialStem[stemIndex]}{_terrestrialBranch[branchIndex]}"); }
/// <summary> /// 获取农历年份 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetYear(DateTime dt) { int yearIndex = china.GetSexagenaryYear(dt); int year = china.GetYear(dt); int yTG = china.GetCelestialStem(yearIndex); int yDZ = china.GetTerrestrialBranch(yearIndex); string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]); return(str); }
//构造函数,根据日期算农历信息 public CnCalendar(DateTime date) { var myDate = date; ChineseLunisolarCalendar caleCn = new ChineseLunisolarCalendar(); //计算农历年份 _sexagenaryYear = caleCn.GetSexagenaryYear(myDate); //计算天干 _celestialStem = caleCn.GetCelestialStem(_sexagenaryYear); //计算地支 _terrestrialBranch = caleCn.GetTerrestrialBranch(_sexagenaryYear); //计算农历月份 _cnMonth = caleCn.GetMonth(myDate); //农历闰月 if (myDate.Month < 3 && _cnMonth > 9) { _cnLeapMonth = caleCn.GetLeapMonth(myDate.Year - 1); } else { _cnLeapMonth = caleCn.GetLeapMonth(myDate.Year); } //计算日子 _cnDay = caleCn.GetDayOfMonth(myDate); }
private void ConvertToGanZhi(DateTime dateTime) { try { if (dateTime < new DateTime(1800, 1, 1, 0, 0, 0) || dateTime > new DateTime(2100, 12, 31, 23, 59, 59)) { throw new DateTimeOutOfRangeException("超出时间允许范围【1800年1月1日-2100年12月31日】!"); } ChineseLunisolarCalendar chineseLunisolarCalendar = new ChineseLunisolarCalendar(); // 年干支 int sexagenaryYear = chineseLunisolarCalendar.GetSexagenaryYear(new DateTime(dateTime.Year, 6, 6)); var liChunDay = SolarTermDictionary.FirstOrDefault(m => m.Key.Year == dateTime.Year && m.Value == SolarTerm.立春); if (dateTime < liChunDay.Key.Date) { sexagenaryYear -= 1; } string tianGanYear = TianGan[chineseLunisolarCalendar.GetCelestialStem(sexagenaryYear)]; string diZhiYear = DiZhi[chineseLunisolarCalendar.GetTerrestrialBranch(sexagenaryYear)]; // 月干支 var nextSolarTerm = SolarTermDictionary.Where(m => m.Key.Year == dateTime.Year && m.Key.Date > dateTime.Date).OrderBy(m => m.Key); int chineseMonth = 0; if (nextSolarTerm.Count() > 0) { chineseMonth = ((int)nextSolarTerm.FirstOrDefault().Value - 1) / 2 + 1; } else { chineseMonth = 11; } string tianGanMonth = GetTianGanMonth(tianGanYear, chineseMonth); string diZhiMonth = ChineseMonth[chineseMonth]; // 日干支 1800年1月1日00:00 庚寅日 丙子时 var timeSpan = dateTime - (new DateTime(1800, 1, 1, 0, 0, 0).AddHours(-1)); string tianGanDay = TianGan[((int)(timeSpan.TotalDays % 10) + 7) % 10]; string diZhiDay = DiZhi[((int)(timeSpan.TotalDays % 12) + 3) % 12]; // 时干支 string tianGanHour = TianGan[((int)(timeSpan.TotalHours / 2 % 10) + 3) % 10]; string diZhiHour = DiZhi[((int)(timeSpan.TotalHours / 2 % 12) + 1) % 12]; TianGanYear = tianGanYear; TianGanMonth = tianGanMonth; TianGanDay = tianGanDay; TianGanHour = tianGanHour; DiZhiYear = diZhiYear; DiZhiMonth = diZhiMonth; DiZhiDay = diZhiDay; DiZhiHour = diZhiHour; } catch (Exception) { throw; } }
private string GetStemBench(DateTime time) { int sexagenaryYear = calendar.GetSexagenaryYear(time);//获取当前日期的年份 //将年份转换为干支纪年 string stemBranch = CelestialStem.Substring(calendar.GetCelestialStem(sexagenaryYear) - 1, 1) + TerrestialBranch.Substring(calendar.GetTerrestrialBranch(sexagenaryYear) - 1, 1); return(stemBranch);//返回干支纪年 }
/// <summary> /// Gets Chinese Sexagenary Year<br /> /// 获得干支年 /// </summary> /// <param name="calendar"></param> /// <param name="dt"></param> /// <param name="traditionalChineseCharacters"></param> /// <returns></returns> public static string GetSexagenaryYear(ChineseLunisolarCalendar calendar, DateTime dt, bool traditionalChineseCharacters = false) { var ganP = traditionalChineseCharacters ? GANZ : GANS; var zhiP = traditionalChineseCharacters ? ZHIZ : ZHIS; var sexagenaryYear = calendar.GetSexagenaryYear(dt); var stemIndex = calendar.GetCelestialStem(sexagenaryYear) - 1; var branchIndex = calendar.GetTerrestrialBranch(sexagenaryYear) - 1; return($"{ganP[stemIndex]}{zhiP[branchIndex]}年"); }
/// <summary> /// 中国农历 /// </summary> static void ChineseDateTimeTest() { Console.WriteLine("天干地支:"); string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; var calendar = new ChineseLunisolarCalendar(); var i = calendar.GetSexagenaryYear(DateTime.Now); var t = calendar.GetCelestialStem(i) - 1; var d = calendar.GetTerrestrialBranch(i) - 1; Console.WriteLine($"{tiangan[t]}{dizhi[d]}年"); }
/// <summary> /// 获取农历年份 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetYear(DateTime dt) { var yearIndex = China.GetSexagenaryYear(dt); const string yearTg = " 甲乙丙丁戊己庚辛壬癸"; const string yearDz = " 子丑寅卯辰巳午未申酉戌亥"; const string yearSx = " 鼠牛虎兔龙蛇马羊猴鸡狗猪"; var year = China.GetYear(dt); var yTg = China.GetCelestialStem(yearIndex); var yDz = China.GetTerrestrialBranch(yearIndex); var str = string.Format("[{1}]{2}{3}{0}", year, yearSx[yDz], yearTg[yTg], yearDz[yDz]); return(str); }
/// 获取农历年份 public static string GetYear(DateTime dt) { int yearIndex = china.GetSexagenaryYear(dt); string yearTG = " 甲乙丙丁戊己庚辛壬癸"; string yearDZ = " 子丑寅卯辰巳午未申酉戌亥"; string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪"; int year = china.GetYear(dt); int yTG = china.GetCelestialStem(yearIndex); int yDZ = china.GetTerrestrialBranch(yearIndex); string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]); return(str); }