C#处理生日提醒功能,最全面代码。

 以下是我经过 一个多星期,摸索出来,考虑的也比较全面的 生日提醒代码,因为生日提醒涉及到 有的是公历,有的农历,还有2月29日 出生,日期处理

总的思路

一.把农历转成公历

二.再分别对待公历生日,农历生日处理。


    protected void TongYi()
    {

 


        DataSet MyDataSet = DBUtility.DbHelperSQL.GetDataSet("select * from ERPUser where  DATEDIFF ( DAY , GETDATE() , TongYi )<0    and  ZaiGang='在岗'  ");

        for (int j = 0; j < MyDataSet.Tables[0].Rows.Count; j++)
        {


            BLL.ERPUser MyModel = new BLL.ERPUser();


            MyModel.ID = int.Parse(MyDataSet.Tables[0].Rows[j]["ID"].ToString());

            if (MyDataSet.Tables[0].Rows[j]["PanDuan"].ToString() == "公历")
            {
                #region 公历生日


                string gonglitongyi = "2014-5-12";
                DateTime dt2 = Convert.ToDateTime(MyDataSet.Tables[0].Rows[j]["BirthDay"].ToString());
                int pox = toResult(MyDataSet.Tables[0].Rows[j]["BirthDay"].ToString());

                if (pox <= 0)
                {
                    #region
                    if ((dt2.Month == 2) && (dt2.Day == 29))
                    {

                        gonglitongyi = DateTime.Now.Year.ToString() + "-" + dt2.Month.ToString() + "-" + "28";

                    }
                    else
                    {

                        gonglitongyi = DateTime.Now.Year.ToString() + "-" + dt2.Month.ToString() + "-" + dt2.Day.ToString();

                    }

                    #endregion

                }
                else
                {

                    #region
                    if ((dt2.Month == 2) && (dt2.Day == 29))
                    {

                        gonglitongyi = (Convert.ToInt32(DateTime.Now.Year.ToString()) + 1).ToString() + "-" + dt2.Month.ToString() + "-" + "28";

                    }
                    else
                    {

                        gonglitongyi = (Convert.ToInt32(DateTime.Now.Year.ToString()) + 1).ToString() + "-" + dt2.Month.ToString() + "-" + dt2.Day.ToString();

                    }
                    #endregion


                }

 

                MyModel.TongYi = gonglitongyi;
                MyModel.NianDai = DateTime.Now.Year.ToString();

                #endregion


            }
            else if (MyDataSet.Tables[0].Rows[j]["PanDuan"].ToString() == "农历")
            {
                #region

                string riqi = MyDataSet.Tables[0].Rows[j]["NongLi"].ToString();
                string moy = "9-18";
                string[] arr = riqi.Split('-');
                if (arr.Length.ToString() == "3")
                {

                    moy = arr[1] + "-" + arr[2];


                }
                arr = null;

                ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
                int niandai = Convert.ToInt32(DateTime.Now.Year.ToString());
                string biaozhun = ChineseCalendar.GetYear(DateTime.Now).ToString();//取农历年
                string vtx2 = biaozhun + "-" + moy.ToString();
                DateTime dt2 = Convert.ToDateTime(vtx2);
                string sss = calendarChineseLunisolarToSolar(dt2).ToString();
                int result = toResult2(sss);
                if (result < 0)
                {

                    biaozhun = (ChineseCalendar.GetYear(DateTime.Now) + 1).ToString();

                }
                else
                {

                    biaozhun = ChineseCalendar.GetYear(DateTime.Now).ToString(); //取农历年

                }
                string vtx3 = biaozhun + "-" + moy.ToString();
                DateTime dt3 = Convert.ToDateTime(vtx3);
                string tongyidate = calendarChineseLunisolarToSolar(dt3).Year.ToString() + "-" + calendarChineseLunisolarToSolar(dt3).Month.ToString() + "-" + calendarChineseLunisolarToSolar(dt3).Day.ToString();
                MyModel.TongYi = tongyidate;
                MyModel.NianDai = DateTime.Now.Year.ToString();

                #endregion

            }

            MyModel.Update2();

        }
    }


    public int toResult(string d2)
    {
        string joinDate1 = "";

        DateTime dt2 = Convert.ToDateTime(d2);

        if ((dt2.Month == 2) && (dt2.Day == 29))
        {

            joinDate1 = (DateTime.Now.Year.ToString() + "-" + dt2.Month.ToString() + "-" + "28");

        }
        else
        {

            joinDate1 = (DateTime.Now.Year.ToString() + "-" + dt2.Month.ToString() + "-" + dt2.Day.ToString());

        }

        DateTime joinDate = Convert.ToDateTime(joinDate1);

 

        TimeSpan ts = DateTime.Now - joinDate;

        return ts.Days;
    }

    public int toResult2(string d2)
    {
        DateTime joinDate = Convert.ToDateTime(d2);

        TimeSpan ts = joinDate - DateTime.Now;

        return ts.Days;
    } 

 

 


    /// <summary>
    /// 将  农历(阴历)   转换为   公历(阳历,西历)
    ///    如果传入的参数中的年份是润年,需要另外进行加上润月的天数,简单测试,但没有做过多的测试,不知是否完全正确
    ///
    ///    DateTime dt = Convert.ToDateTime("2010-05-21 00:00:00");
    ///    MessageBox.Show(calendarChineseLunisolarToSolar(dt).ToString());
    ///   
    /// </summary>
    ///
    public static DateTime calendarChineseLunisolarToSolar(DateTime ChineseLunisolarDateTime)
    {
        System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();

        if (ChineseLunisolarDateTime.Year < 1902 || ChineseLunisolarDateTime.Year > 2100)
            throw new Exception("只支持1902~2100期间的农历年");

        DateTime dt = cal.ToDateTime(ChineseLunisolarDateTime.Year, ChineseLunisolarDateTime.Month, ChineseLunisolarDateTime.Day, 0, 0, 0, 0);

 


        //检测是否含有润月
        int leapMonth = cal.GetLeapMonth(ChineseLunisolarDateTime.Year);

        int leapMonthInDays = 0;

        if ((leapMonth > 0) & (leapMonth <= ChineseLunisolarDateTime.Month))
        // if (leapMonth > 0 & ChineseLunisolarDateTime.Month > leapMonth)
        {
            //有润月,则读到这个润月里面的天数
            leapMonthInDays = cal.GetDaysInMonth(ChineseLunisolarDateTime.Year, ChineseLunisolarDateTime.Month);
        }

        dt = dt.AddDays(leapMonthInDays);

        return dt;
    }

posted on 2015-03-06 16:42  stay cool  阅读(1562)  评论(0编辑  收藏  举报

导航