計算年齡

            根據出生日期,計算年齡(距當前時間),調用:Age(20121202);    方法如下:

     private void Age(string Date)
        {
            string RValue = string.Empty;

            string NowDate = DateTime.Now.ToString("yyyyMMdd");
            DateTime BrithDate = DateTime.Parse(Date.Insert(6, "-").Insert(4, "-"));


            if (DateTime.Now.Year == BrithDate.Year) //年份相等
            {
                #region
                if (DateTime.Now.Month == BrithDate.Month)  //月份相等
                {
                    RValue = (DateTime.Now.Day - BrithDate.Day).ToString() + "天";
                }
                else  //月份不相等
                {
                    if (DateTime.Now.Day >= BrithDate.Day)
                    {
                        RValue = (DateTime.Now.Month - BrithDate.Month).ToString() + "月";

                    }
                    else
                    {
                        RValue = (DateTime.Now.Month - BrithDate.Month - 1).ToString() + "月";
                    }

                }
                #endregion

            }
            else  //年份不相等
            {
                #region

                if (DateTime.Now.Month > BrithDate.Month)
                {
                    if (DateTime.Now.Day >= BrithDate.Day)
                          RValue = (DateTime.Now.Year - BrithDate.Year).ToString() + "年"+(DateTime.Now.Month-BrithDate.Month).ToString()+"月";
                    else
                        RValue = (DateTime.Now.Year - BrithDate.Year).ToString() + "年" + (DateTime.Now.Month - BrithDate.Month-1).ToString() + "月";
                }
                else if (DateTime.Now.Month < BrithDate.Month)
                {
                    if (DateTime.Now.Year - BrithDate.Year - 1 == 0)
                    { //eg:出生日期:20111020,當前日期:20120820
                        if (DateTime.Now.Day >= BrithDate.Day)
                        {
                            RValue = (12 - BrithDate.Month + DateTime.Now.Month ).ToString() + "月";

                        }
                        else
                        {
                            RValue = (12 - BrithDate.Month + DateTime.Now.Month - 1).ToString() + "月";
                        }

                    }
                    else
                    {
                        RValue = (DateTime.Now.Year - BrithDate.Year - 1).ToString() + "年" + (12- BrithDate.Month +DateTime.Now.Month - 1).ToString() + "月";
                    }
                }
                else  //年份不一樣,月份相等
                {
                    if (DateTime.Now.Day >= BrithDate.Day)
                    {
                        RValue = (DateTime.Now.Year - BrithDate.Year).ToString() + "年";

                    }
                    else
                    {
                        if (DateTime.Now.Year - BrithDate.Year - 1 == 0)
                        { //eg:出生日期:20111020,當前日期:20120820
                            if (DateTime.Now.Day >= BrithDate.Day)
                            {
                                RValue = (DateTime.Now.Year - BrithDate.Year).ToString() + "年";

                            }
                            else
                            {
                                RValue = (12 - BrithDate.Month + DateTime.Now.Month - 1).ToString() + "月";
                            }

                        }
                        else
                        {
                            RValue = (DateTime.Now.Year - BrithDate.Year - 1).ToString() + "年";
                        }
                    }
                }

                #endregion


            }
            MessageBox.Show(RValue.ToString());
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章