C#根据年份获取十二生肖

发布时间 2023-05-24 21:22:07作者: 夜店耍流氓

第一步:首先引入命名空间

using System.Globalization;

 

第二步:编写获取方法

public string GetChineseZodiac(DateTime time)
{
   ChineseLunisolarCalendar chineseLunisolarCalendar = new ChineseLunisolarCalendar();
   int year = chineseLunisolarCalendar.GetSexagenaryYear(time);
   string treeYear = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
   string chineseZodiac = treeYear.Substring(chineseLunisolarCalendar.GetTerrestrialBranch(year) - 1, 1);
   return chineseZodiac;
}