欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的阴历阳历互相转化类实例

程序员文章站 2023-11-26 23:23:04
本文实例讲述了c#实现的阴历阳历互相转化类。分享给大家供大家参考,具体如下: 最近郁闷地发现网上现有的相当一部分万年历上干支纪年的算法都是错误的。因为干支纪年是针对阴历而...

本文实例讲述了c#实现的阴历阳历互相转化类。分享给大家供大家参考,具体如下:

最近郁闷地发现网上现有的相当一部分万年历上干支纪年的算法都是错误的。因为干支纪年是针对阴历而言的,而生肖属相又跟地支对应,所以元旦和春节之间那段时间在干支纪年法中应该归上一年,以阳历2007年2月9日为例,当日的阴历日期是二〇〇六年十二月廿二日,是丙戌年,即狗年,但是浏览一下目前的万年历,相当一部分都显示成了丁亥年,猪年,比较郁闷~~

然后就写了一个阴历阳历互相转化的类。

C#实现的阴历阳历互相转化类实例

相关代码如下:

/// <summary>
/// 中国日历信息实体类
/// </summary>
public sealed class chinesecalendarinfo
{
    private datetime m_solardate;
    private int m_lunaryear, m_lunarmonth, m_lunarday;
    private bool m_isleapmonth = false;
    private string m_lunaryearsexagenary = null, m_lunaryearanimal = null;
    private string m_lunaryeartext = null, m_lunarmonthtext = null, m_lunardaytext = null;
    private string m_solarweektext = null, m_solarconstellation = null, m_solarbirthstone = null;
    日历属性
    /// <summary>
    /// 根据指定阳历日期计算星座&诞生石
    /// </summary>
    /// <param name="date">指定阳历日期</param>
    /// <param name="constellation">星座</param>
    /// <param name="birthstone">诞生石</param>
    public static void calcconstellation(datetime date, out string constellation, out string birthstone)
    {
      int i = convert.toint32(date.tostring("mmdd"));
      int j;
      if (i >= 321 && i <= 419)
        j = 0;
      else if (i >= 420 && i <= 520)
        j = 1;
      else if (i >= 521 && i <= 621)
        j = 2;
      else if (i >= 622 && i <= 722)
        j = 3;
      else if (i >= 723 && i <= 822)
        j = 4;
      else if (i >= 823 && i <= 922)
        j = 5;
      else if (i >= 923 && i <= 1023)
        j = 6;
      else if (i >= 1024 && i <= 1121)
        j = 7;
      else if (i >= 1122 && i <= 1221)
        j = 8;
      else if (i >= 1222 || i <= 119)
        j = 9;
      else if (i >= 120 && i <= 218)
        j = 10;
      else if (i >= 219 && i <= 320)
        j = 11;
      else
      {
        constellation = "未知星座";
        birthstone = "未知诞生石";
        return;
      }
      constellation = constellations[j];
      birthstone = birthstones[j];
      星座划分
    }
    阴历转阳历
    从阴历创建日历
    private static chineselunisolarcalendar calendar = new chineselunisolarcalendar();
    public const string chinesenumber = "〇一二三四五六七八九";
    public const string celestialstem = "甲乙丙丁戊己庚辛壬癸";
    public const string terrestrialbranch = "子丑寅卯辰巳午未申酉戌亥";
    public const string animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
    public static readonly string[] chineseweekname = new string[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
    public static readonly string[] chinesedayname = new string[] {
      "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
      "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
      "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
    public static readonly string[] chinesemonthname = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
    public static readonly string[] constellations = new string[] { "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座" };
    public static readonly string[] birthstones = new string[] { "钻石", "蓝宝石", "玛瑙", "珍珠", "红宝石", "红条纹玛瑙", "蓝宝石", "猫眼石", "黄宝石", "土耳其玉", "紫水晶", "月长石,血石" };
}

附:完整实例代码点击此处本站下载

ps:这里再为大家推荐几款日历相关在线工具供大家参考:

网页万年历日历:

在线阴历/阳历转换工具:

在线万年历日历:

在线万年历黄历flash版:

另外,本站历史上的今天也有相似的农历日期显示功能:

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#日期与时间操作技巧总结》、《c#字符串操作技巧总结》、《c#数组操作技巧总结》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。