程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 簡單的日期轉換,簡單日期轉換

簡單的日期轉換,簡單日期轉換

編輯:C#入門知識

簡單的日期轉換,簡單日期轉換


 

private void button1_Click(object sender, EventArgs e){
               DateTime nDate = Convert.ToDateTime(dateTimePicker1.Text); //當前需要轉換的日期
               string jrDate = ChinaDate.GetChinaHoliday(nDate); //獲取農歷節日
               string jrMonth = ChinaDate.GetMonth(nDate); //獲取農歷月份
               string jrDay = ChinaDate.GetDay(nDate); //獲取農歷日期            
               DateTime jrSDate = ChinaDate.GetSunYearDate(nDate); //陽歷轉陰歷  
               string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
               string week = weekdays[Convert.ToInt32(nDate.DayOfWeek)];
               this.textBox1.Text = jrSDate.ToString();     
        }
    
        /// <summary>
        /// 農歷與陰歷之間相互轉化工具類
        /// </summary>
        public class ChinaDate
        {
            #region 農歷信息獲取
            private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar();
            private static Hashtable gHoliday = new Hashtable();
            private static Hashtable nHoliday = new Hashtable();
            private static string[] JQ = { "小寒", "大寒", "立春", "雨水", "驚蟄", "春分", "清明", "谷雨", "立夏", "小滿", "芒種", "夏至", "小暑", "大暑", "立秋", "處暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
            private static int[] JQData = { 0, 21208, 43467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };

            static ChinaDate(){
                //公歷節日
                gHoliday.Add("0101", "元旦");
                gHoliday.Add("0214", "情人節");
                gHoliday.Add("0305", "雷鋒日");
                gHoliday.Add("0308", "婦女節");
                gHoliday.Add("0312", "植樹節");
                gHoliday.Add("0315", "消費者權益日");
                gHoliday.Add("0401", "愚人節");
                gHoliday.Add("0501", "勞動節");
                gHoliday.Add("0504", "青年節");
                gHoliday.Add("0601", "兒童節");
                gHoliday.Add("0701", "建黨節");
                gHoliday.Add("0801", "建軍節");
                gHoliday.Add("0910", "教師節");
                gHoliday.Add("1001", "國慶節");
                gHoliday.Add("1224", "平安夜");
                gHoliday.Add("1225", "聖誕節");
                //農歷節日
                nHoliday.Add("0101", "春節");
                nHoliday.Add("0115", "元宵節");
                nHoliday.Add("0505", "端午節");
                nHoliday.Add("0815", "中秋節");
                nHoliday.Add("0909", "重陽節");
                nHoliday.Add("1208", "臘八節");
            }

            /// <summary>
            /// 獲取農歷
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetChinaDate(DateTime dt){
                if (dt > china.MaxSupportedDateTime || dt < china.MinSupportedDateTime){
                    //日期范圍:1901 年 2 月 19 日 - 2101 年 1 月 28 日
                    throw new Exception(string.Format("日期超出范圍!必須在{0}到{1}之間!", china.MinSupportedDateTime.ToString("yyyy-MM-dd"), china.MaxSupportedDateTime.ToString("yyyy-MM-dd")));
                }
                string str = string.Format("{0} {1}{2}", GetYear(dt), GetMonth(dt), GetDay(dt));
                string strJQ = GetSolarTerm(dt);
                if (strJQ != ""){
                    str += " (" + strJQ + ")";
                }
                string strHoliday = GetHoliday(dt);
                if (strHoliday != ""){
                    str += " " + strHoliday;

                }
                string strChinaHoliday = GetChinaHoliday(dt);
                if (strChinaHoliday != ""){
                    str += " " + strChinaHoliday;
                }
                return str;
            }

            /// <summary>
            /// 獲取農歷年份
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetYear(DateTime dt)
            {
                int yearIndex = china.GetSexagenaryYear(dt);
                string yearTG = " 甲乙丙丁戊己庚辛壬癸";
                string yearDZ = " 子丑寅卯辰巳午未申酉戌亥";
                string yearSX = " 鼠牛虎兔龍蛇馬羊猴雞狗豬";
                int year = china.GetYear(dt);
                int yTG = china.GetCelestialStem(yearIndex);
                int yDZ = china.GetTerrestrialBranch(yearIndex);
                string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]);
                return str;
            }

            /// <summary>
            /// 獲取農歷月份
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetMonth(DateTime dt){
                int year = china.GetYear(dt);
                int iMonth = china.GetMonth(dt);
                int leapMonth = china.GetLeapMonth(year);
                bool isLeapMonth = iMonth == leapMonth;
                if (leapMonth != 0 && iMonth >= leapMonth){
                    iMonth--;
                }
                string szText = "正二三四五六七八九十";
                string strMonth = isLeapMonth ? "閏" : "";
                if (iMonth <= 10){
                    strMonth += szText.Substring(iMonth - 1, 1);
                }
                else if (iMonth == 11){
                    strMonth += "十一";
                }
                else{
                    strMonth += "臘";
                }
                return strMonth + "月";
            }

            /// <summary>
            /// 獲取農歷日期
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetDay(DateTime dt){
                int iDay = china.GetDayOfMonth(dt);
                string szText1 = "初十廿三";
                string szText2 = "一二三四五六七八九十";
                string strDay;
                if (iDay == 20){
                    strDay = "二十";
                }
                else if (iDay == 30){
                    strDay = "三十";
                }
                else{
                   strDay = szText1.Substring((iDay - 1) / 10, 1);
                   strDay = strDay + szText2.Substring((iDay - 1) % 10, 1);
                }
                return strDay;
            }

            /// <summary>
            /// 獲取節氣
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetSolarTerm(DateTime dt){
                DateTime dtBase = new DateTime(1900, 1, 6, 2, 5, 0);
                DateTime dtNew;
                double num;
                int y;
                string strReturn = "";
                y = dt.Year;
                for (int i = 1; i <= 24; i++){
                    num = 525948.76 * (y - 1900) + JQData[i - 1];
                    dtNew = dtBase.AddMinutes(num);
                    if (dtNew.DayOfYear == dt.DayOfYear){
                        strReturn = JQ[i - 1];
                    }
                }
                return strReturn;
            }

            /// <summary>
            /// 獲取公歷節日
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetHoliday(DateTime dt){
                string strReturn = "";
                object g = gHoliday[dt.Month.ToString("00") + dt.Day.ToString("00")];
                if (g != null){
                    strReturn = g.ToString();
                }
                return strReturn;
            }


            /// <summary>
            /// 獲取農歷節日
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetChinaHoliday(DateTime dt){
                string strReturn = "";
                int year = china.GetYear(dt);
                int iMonth = china.GetMonth(dt);
                int leapMonth = china.GetLeapMonth(year);
                int iDay = china.GetDayOfMonth(dt);
                if (china.GetDayOfYear(dt) == china.GetDaysInYear(year)){
                    strReturn = "除夕";
                }
                else if (leapMonth != iMonth){
                    if (leapMonth != 0 && iMonth >= leapMonth){
                        iMonth--;
                    }
                    object n = nHoliday[iMonth.ToString("00") + iDay.ToString("00")];
                    if (n != null){
                        if (strReturn == ""){
                            strReturn = n.ToString();
                        }
                        else{
                            strReturn += " " + n.ToString();
                        }
                    }
                }
                return strReturn;
            }
            #endregion





            #region 陰歷-陽歷-轉換

            /// <summary>
            /// 陰歷轉為陽歷
            /// </summary>
            /// <param name="year">指定的年份</param>
            public static DateTime GetLunarYearDate(DateTime dt){
                int cnYear = china.GetYear(dt);
                int cnMonth = china.GetMonth(dt);
                int num1 = 0;
                int num2 = china.IsLeapYear(cnYear) ? 13 : 12;
                while (num2 >= cnMonth){
                    num1 += china.GetDaysInMonth(cnYear, num2--);
                }
                num1 = num1 - china.GetDayOfMonth(dt) + 1;
                return dt.AddDays(num1);
            }

            /// <summary>
            /// 陽歷轉為陰歷
            /// </summary>
            /// <param name="dt">公歷日期</param>
            /// <returns>農歷的日期</returns>
            public static DateTime GetSunYearDate(DateTime dt){
                int year = china.GetYear(dt);
                int iMonth = china.GetMonth(dt);
                int iDay = china.GetDayOfMonth(dt);
                int leapMonth = china.GetLeapMonth(year);
                bool isLeapMonth = iMonth == leapMonth;
                if (leapMonth != 0 && iMonth >= leapMonth){
                    iMonth--;
                }
                string str = string.Format("{0}-{1}-{2}", year, iMonth, iDay);
                DateTime dtNew = DateTime.Now;
                try{
                    dtNew = Convert.ToDateTime(str); //防止出現2月份時,會出現超過時間,出現“2015-02-30”這種錯誤日期
                }
                catch
                {}
                return dtNew;
            }
            #endregion
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved