之前写过一篇关于C#获取到农历相关的,今天又研究了一下,再写一篇
代码:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace LingbugUtils.Models
{
public class ChineseCalendarModel
{
private string[] HeavenlyStemList;
private string[] EarthlyBranchList;
private string[] AnimalList;
private string[] MonthList;
private string[] DayFirstNameList;
private string[] DayLastNameList;
private ConstellationModel[] ConstellationList;
public ChineseCalendarModel(DateTime date)
{
InitBaseData();
Init(date);
}
private void InitBaseData()
{
char o = '、';
//十天干
string a = "甲、乙、丙、丁、戊、己、庚、辛、壬、癸";
this.HeavenlyStemList = a.Split(o);
//十二地支
string b = "子、丑、寅、卯、辰、巳、午、未、申、酉、戌、亥";
this.EarthlyBranchList = b.Split(o);
//十二生肖
string c = "鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪";
this.AnimalList = c.Split(o);
//月
string d = "正、二、三、四、五、六、七、八、九、十、十一、腊";
this.MonthList = d.Split(o);
//日-开头
string e = "初、十、甘、三";
this.DayFirstNameList = e.Split(o);
//日-结尾
string f = "一、二、三、四、五、六、七、八、九、十";
this.DayLastNameList = f.Split(o);
//星座
var constellationList = new List<ConstellationModel>()
{
new ConstellationModel("白羊", "3-21", "4-19"),
new ConstellationModel("金牛", "4-20", "5-20"),
new ConstellationModel("双子", "5-21", "6-21"),
new ConstellationModel("巨蟹", "6-22", "7-22"),
new ConstellationModel("狮子", "7-23", "8-22"),
new ConstellationModel("处女", "8-23", "9-22"),
new ConstellationModel("天秤", "9-23", "10-23"),
new ConstellationModel("天蝎", "10-24", "11-22"),
new ConstellationModel("射手", "11-23", "12-21"),
new ConstellationModel("摩羯", "12-22", "1-19"),
new ConstellationModel("水瓶", "1-20", "2-18"),
new ConstellationModel("双鱼", "2-19", "3-20")
};
this.ConstellationList = constellationList.ToArray();
}
private void Init(DateTime date)
{
this.Argument = date;
var calendarService = new ChineseLunisolarCalendar();
//农历年
this.Year = calendarService.GetYear(this.Argument);
//农历月
int month = calendarService.GetMonth(this.Argument);
//今年是否有闰月
if (calendarService.IsLeapYear(this.Year))
{
//有闰月
//获取到闰几月(这里-1是因为,如果是7,则日历会显示闰6月,所以-1)
int leapMonth = calendarService.GetLeapMonth(this.Year) - 1;
//如果当前月比闰月大,都要-1(因为多闰了一个月)
this.Month = month > leapMonth ? month - 1 : month;
}
else
{
//没有闰月,月份就是正确的
this.Month = month;
}
//农历日
this.Day = calendarService.GetDayOfMonth(this.Argument);
//农历年索引(后续获取天干地支生肖会使用到)
int yearIndex = calendarService.GetSexagenaryYear(this.Argument);
//天干
this.HeavenlyStem = this.HeavenlyStemList[calendarService.GetCelestialStem(yearIndex) - 1];
//地支和生肖索引一样
int animalIndex = calendarService.GetTerrestrialBranch(yearIndex) - 1;
this.EarthlyBranch = this.EarthlyBranchList[animalIndex];
this.Animal = this.AnimalList[animalIndex];
//星座(因为涉及到跨年,所以只要没找到,那肯定是跨年的那个星座,就肯定是摩羯座)
var constellationModel = this.ConstellationList.FirstOrDefault(r => this.Argument >= r.Begin && this.Argument <= r.End);
this.ConstellationName = constellationModel == null ? "摩羯座" : constellationModel.Name;
//月显示
string monthStr = this.MonthList[this.Month - 1];
//如果是闰月,加上闰
if (calendarService.IsLeapMonth(this.Year, month)) monthStr = "闰" + monthStr;
//日显示
string dayStr = string.Empty;
//整十的额外处理
if (this.Day == 10)
{
dayStr = "初十";
}
else if (this.Day == 20)
{
dayStr = "二十";
}
else if (this.Day == 30)
{
dayStr = "三十";
}
else
{
//不是整十的
int dayMod = this.Day % 10;//取余,获取到后面显示
int dayDev = this.Day / 10;//除法,获取到前面显示
//拼接起来
dayStr = this.DayFirstNameList[dayDev] + this.DayLastNameList[dayMod - 1];
}
//完整显示
this.Text = string.Format("{0}{1}{2}年{3}月{4}", this.HeavenlyStem, this.EarthlyBranch, this.Animal, monthStr, dayStr);
}
//参数日期
public DateTime Argument { get; set; }
//农历年
public int Year { get; set; }
//农历月
public int Month { get; set; }
//农历日
public int Day { get; set; }
//天干
public string HeavenlyStem { get; set; }
//地支
public string EarthlyBranch { get; set; }
//生肖
public string Animal { get; set; }
//星座
public string ConstellationName { get; set; }
public string Text { get; set; }
}
}
星座类:
using System;
namespace LingbugUtils.Models
{
public class ConstellationModel
{
public ConstellationModel(string name, string beginStr, string endStr)
{
this.Name = name + "座";
this.Begin = Convert.ToDateTime(Convert.ToDateTime(beginStr).ToString("yyyy-MM-dd 00:00:00.000"));
this.End = Convert.ToDateTime(Convert.ToDateTime(endStr).ToString("yyyy-MM-dd 23:59:59.999"));
}
public string Name { get; set; }
public DateTime Begin { get; set; }
public DateTime End { get; set; }
}
}
Ending~