Exemple #1
0
        /// <summary>
        /// 获取中国农历节日字符串。
        /// </summary>
        public string GetChineseHoliday(DateTime solarDateTime)
        {
            var re        = "";
            int year      = netCalendar.GetYear(solarDateTime);
            int mon       = netCalendar.GetMonth(solarDateTime);
            int leapMonth = netCalendar.GetLeapMonth(year);
            int iDay      = netCalendar.GetDayOfMonth(solarDateTime);

            if (netCalendar.GetDayOfYear(solarDateTime) == netCalendar.GetDaysInYear(year))
            {
                return("除夕");
            }

            if (leapMonth != mon)
            {
                if (leapMonth != 0 && mon >= leapMonth)
                {
                    mon--;
                }

                foreach (var s in chineseHoliday)
                {
                    if (mon == Convert.ToInt32(s.Substring(0, 2)))
                    {
                        if (netCalendar.GetDayOfMonth(solarDateTime) == Convert.ToInt32(s.Substring(2, 2)))
                        {
                            re = s.Substring(4);
                            break;
                        }
                    }
                }
            }

            return(re);
        }
Exemple #2
0
        /// <summary>
        /// 根据公历获取农历日期
        /// </summary>
        /// <param name="datetime">公历日期</param>
        public static string GetChineseDateTime(DateTime datetime)
        {
            System.Globalization.ChineseLunisolarCalendar cCalendar = new System.Globalization.ChineseLunisolarCalendar();
            int lyear  = cCalendar.GetYear(datetime);
            int lmonth = cCalendar.GetMonth(datetime);
            int lday   = cCalendar.GetDayOfMonth(datetime);

            //获取闰月, 0 则表示没有闰月
            int leapMonth = cCalendar.GetLeapMonth(lyear);

            bool isleap = false;

            if (leapMonth > 0)
            {
                if (leapMonth == lmonth)
                {
                    //闰月
                    isleap = true;
                    lmonth--;
                }
                else if (lmonth > leapMonth)
                {
                    lmonth--;
                }
            }
            string ret = string.Concat(GetLunisolarYear(datetime.Year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday));

            ret = "农历" + ret;
            return(ret);
        }
Exemple #3
0
        public static string RiQiGetShengXiao(
            [ExcelArgument(Description = "公历日期")] DateTime solarDate)
        {
            System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();
            int year = cal.GetYear(solarDate);

            return("鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12].ToString());
        }
Exemple #4
0
        public static string RiQiGetGanZhiYear(
            [ExcelArgument(Description = "公历日期")] DateTime solarDate)
        {
            System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();
            int year = cal.GetYear(solarDate);

            char gan = "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10];
            char zhi = "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12];

            return(new string(new char[] { gan, zhi }));
        }
        /// <summary>
        /// 返回指定公历日期的阴历时间
        /// </summary>
        /// <param name="time"></param>
        public ChinaDateTime(DateTime time)
        {
            cc = new System.Globalization.ChineseLunisolarCalendar();

            if (time > cc.MaxSupportedDateTime || time < cc.MinSupportedDateTime)
                throw new Exception("参数日期时间不在支持的范围内,支持范围:" + cc.MinSupportedDateTime.ToShortDateString() + "到" + cc.MaxSupportedDateTime.ToShortDateString());
            year = cc.GetYear(time);
            month = cc.GetMonth(time);
            dayOfMonth = cc.GetDayOfMonth(time);
            isLeap = cc.IsLeapMonth(year, month);
            if (isLeap) month -= 1;
            this.time = time;

        }
Exemple #6
0
        /// <summary>
        /// 将公历(阳历,西历) 转换为 农历(阴历)
        ///
        ///    DateTime dt = Convert.ToDateTime("2010-07-2 00:00:00");
        ///    MessageBox.Show(calendarSolarToChineseLunisolar(dt).ToString());
        ///
        /// </summary>
        ///

        public static DateTime calendarSolarToChineseLunisolar(DateTime SolarDateTime)
        {
            System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();

            int cYear  = cal.GetYear(SolarDateTime);
            int cMonth = cal.GetMonth(SolarDateTime);
            int cDay   = cal.GetDayOfMonth(SolarDateTime);

            DateTime dt = Convert.ToDateTime(
                Convert.ToString(cYear) + "-" +
                Convert.ToString(cMonth) + "-" +
                Convert.ToString(cDay)
                + " 00:00:00");

            return(dt);
        }
        /// <summary>
        /// 返回指定公历日期的阴历时间
        /// </summary>
        /// <param name="time"></param>
        public ChinaDateTime(DateTime time)
        {
            cc = new System.Globalization.ChineseLunisolarCalendar();

            if (time > cc.MaxSupportedDateTime || time < cc.MinSupportedDateTime)
            {
                throw new Exception("参数日期时间不在支持的范围内,支持范围:" + cc.MinSupportedDateTime.ToShortDateString() + "到" + cc.MaxSupportedDateTime.ToShortDateString());
            }
            year       = cc.GetYear(time);
            month      = cc.GetMonth(time);
            dayOfMonth = cc.GetDayOfMonth(time);
            isLeap     = cc.IsLeapMonth(year, month);
            if (isLeap)
            {
                month -= 1;
            }
            this.time = time;
        }
Exemple #8
0
        /// 公历转为农历的函数
        /// </summary>
        /// <remarks>作者:DeltaCat</remarks>
        /// <example>网址:http://www.zu14.cn</example>
        /// <param name="solarDateTime">公历日期</param>
        /// <returns>农历的日期</returns>
        public string SolarToChineseLunisolarDate(DateTime solarDateTime)
        {
            System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();

            int year      = cal.GetYear(solarDateTime);
            int month     = cal.GetMonth(solarDateTime);
            int day       = cal.GetDayOfMonth(solarDateTime);
            int leapMonth = cal.GetLeapMonth(year);

            return(string.Format("农历{0}{1}({2})年{3}{4}月{5}{6}"
                                 , "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10]
                                 , "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12]
                                 , "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12]
                                 , month == leapMonth ? "闰" : ""
                                 , "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]
                                 , "初十廿三"[day / 10]
                                 , "日一二三四五六七八九"[day % 10]
                                 ));
        }
Exemple #9
0
        /// <summary>
        /// 取得当前时间的农历日期
        /// </summary>
        /// <returns></returns>
        public static string CNDate()
        {
            System.Globalization.ChineseLunisolarCalendar l = new System.Globalization.ChineseLunisolarCalendar();
            DateTime dt = DateTime.Today; //转换当日的日期

            string[] aMonth = { "", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "腊月", "腊月" };

            string[] a10 = { "初", "十", "二十", "三十" };
            string[] aDigi = { "O", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            string   sYear = "", sYearArab = "", sMonth = "", sDay = "", sDay10 = "", sDay1 = "", sLuniSolarDate = "";
            int      iYear, iMonth, iDay;

            iYear  = l.GetYear(dt);
            iMonth = l.GetMonth(dt);
            iDay   = l.GetDayOfMonth(dt);

            //Format Year
            sYearArab = iYear.ToString();
            for (int i = 0; i < sYearArab.Length; i++)
            {
                sYear += aDigi[Convert.ToInt16(sYearArab.Substring(i, 1))];
            }

            //Format Month
            int iLeapMonth = l.GetLeapMonth(iYear); //获取闰月

            if (iLeapMonth > 0 && iMonth <= iLeapMonth)
            {
                aMonth[iLeapMonth] = "闰" + aMonth[iLeapMonth - 1];
                sMonth             = aMonth[l.GetMonth(dt)];
            }
            else if (iLeapMonth > 0 && iMonth > iLeapMonth)
            {
                sMonth = aMonth[l.GetMonth(dt) - 1];
            }
            else
            {
                sMonth = aMonth[l.GetMonth(dt)];
            }


            //Format Day
            sDay10 = a10[iDay / 10];
            sDay1  = aDigi[(iDay % 10)];
            sDay   = sDay10 + sDay1;

            if (iDay == 10)
            {
                sDay = "初十";
            }
            if (iDay == 20)
            {
                sDay = "二十";
            }
            if (iDay == 30)
            {
                sDay = "三十";
            }

            //Format Lunar Date
            sLuniSolarDate = sMonth + sDay;
            return(sLuniSolarDate);
        }
Exemple #10
0
        /// <summary>
        /// ȡ�õ�ǰʱ���ũ������
        /// </summary>
        /// <returns></returns>
        public static string CNDate()
        {
            System.Globalization.ChineseLunisolarCalendar l = new System.Globalization.ChineseLunisolarCalendar();
            DateTime dt = DateTime.Today; //ת�����յ�����
            string[] aMonth = { "", "����", "����", "����", "����", "����", "����", "����", "����", "����", "ʮ��", "ʮһ��", "����", "����" };

            string[] a10 = { "��", "ʮ", "��ʮ", "��ʮ" };
            string[] aDigi = { "��", "һ", "��", "��", "��", "��", "��", "��", "��", "��" };
            string sYear = "", sYearArab = "", sMonth = "", sDay = "", sDay10 = "", sDay1 = "", sLuniSolarDate = "";
            int iYear, iMonth, iDay;
            iYear = l.GetYear(dt);
            iMonth = l.GetMonth(dt);
            iDay = l.GetDayOfMonth(dt);

            //Format Year
            sYearArab = iYear.ToString();
            for (int i = 0; i < sYearArab.Length; i++)
            {
                sYear += aDigi[Convert.ToInt16(sYearArab.Substring(i, 1))];
            }

            //Format Month
            int iLeapMonth = l.GetLeapMonth(iYear); //��ȡ����

            if (iLeapMonth > 0 && iMonth <= iLeapMonth)
            {
                aMonth[iLeapMonth] = "��" + aMonth[iLeapMonth - 1];
                sMonth = aMonth[l.GetMonth(dt)];
            }
            else if (iLeapMonth > 0 && iMonth > iLeapMonth)
            {
                sMonth = aMonth[l.GetMonth(dt) - 1];
            }
            else
            {
                sMonth = aMonth[l.GetMonth(dt)];
            }

            //Format Day
            sDay10 = a10[iDay / 10];
            sDay1 = aDigi[(iDay % 10)];
            sDay = sDay10 + sDay1;

            if (iDay == 10) sDay = "��ʮ";
            if (iDay == 20) sDay = "��ʮ";
            if (iDay == 30) sDay = "��ʮ";

            //Format Lunar Date
            sLuniSolarDate = sMonth + sDay;
            return sLuniSolarDate;
        }