Exemplo n.º 1
0
        public String GetBirthDate(DateType type, DateFormat format)
        {
            string result = "";

            switch (type)
            {
            case DateType.Calendar:
                result = (format == DateFormat.ShortDate) ? this.birthDate.ToString("d") : this.birthDate.ToString("D");
                break;

            case DateType.ChineseCalendar:
                //System.Globalization.TaiwanCalendar
                TaiwanCalendar c1 = new TaiwanCalendar();
                int            y  = c1.GetYear(this.birthDate);
                int            m  = c1.GetMonth(this.birthDate);
                int            d  = c1.GetDayOfMonth(this.birthDate);
                result = (format == DateFormat.ShortDate) ? String.Format("{0:000}/{1:00}/{2:00}", y, m, d) : String.Format("民國{0}年{1}月{2}日", y, m, d);
                break;

            case DateType.ChineseLunisolar:
                //System.Globalization.TaiwanLunisolarCalendar
                TaiwanLunisolarCalendar c2 = new TaiwanLunisolarCalendar();
                int y2 = c2.GetYear(this.birthDate);
                int m2 = c2.GetMonth(this.birthDate);
                int d2 = c2.GetDayOfMonth(this.birthDate);
                result = (format == DateFormat.ShortDate) ? String.Format("{0:000}/{1:00}/{2:00}", y2, m2, d2) : String.Format("農曆{0}年{1}月{2}日", y2, m2, d2);
                break;

            default:
                break;
            }
            return(result);
        }
Exemplo n.º 2
0
    public static string useTaiwanLC(DateTime date)
    {
        string TeanGean = "甲乙丙丁戊己庚辛壬癸";
        string DeGe     = "子丑寅卯辰巳午未申酉戌亥";
        string CAnimal  = "鼠牛虎兔龍蛇馬羊猴雞狗豬";

        TaiwanLunisolarCalendar Tlc = new TaiwanLunisolarCalendar();
        //DateTime dtNow = new DateTime.Now();//拿今年的日期
        //DateTime.Now.AddYears;


        int lun60Year = Tlc.GetSexagenaryYear(date.AddYears(0));
        //  int lun60Year = Tlc.GetSexagenaryYear(DateTime.Now.AddYears(0));
        int TeanGeanYear = Tlc.GetCelestialStem(lun60Year) - 1;
        int DeGeYear     = Tlc.GetTerrestrialBranch(lun60Year) - 1;

        int lunMonth  = Tlc.GetMonth(DateTime.Now.AddYears(0));
        int leapMonth = Tlc.GetLeapMonth(Tlc.GetYear(DateTime.Now.AddYears(0)));

        if (leapMonth > 0 && lunMonth >= leapMonth)
        {
            //lunMonth = lunMonth - 1;
            lunMonth -= 1;
        }
        int lunDay = Tlc.GetDayOfMonth(DateTime.Now.AddYears(0));

        //Console.WriteLine("debug" + TeanGean[TeanGeanYear] + DeGe[DeGeYear] + CAnimal[DeGeYear]);
        Console.WriteLine("驗算網頁 : http://tlcheng.twbbs.org/Model/Online/LunarCalendar/Default.aspx");
        Console.WriteLine("參考資料 : http://anita-lo.blogspot.com/2008/03/net_20.html");
        Console.WriteLine("查表法   : http://chmis.cca.gov.tw/chmp/blog/a571024/myBlogArticleAction.do?method=doListArticleByPk&articleId=3213");
        Console.WriteLine("參考資料 : http://www.fushantang.com/1003/c1007.html");
        //return String.Format("農曆:{0}年{1}月{2}日", TeanGean[TeanGeanYear] & DeGe[DeGeYear], lunMonth, lunDay);
        //return String.Format("農曆:{0}年{1}月{2}日 今年的生肖: {3}",Tlc.GetYear(DateTime.Now).ToString(), lunMonth, lunDay, CAnimal[DeGeYear].ToString());
        return(String.Format("{0}", CAnimal[DeGeYear].ToString()));
    }
Exemplo n.º 3
0
        /// <summary>
        /// Convert the DateTime object to TaiwanLunisolarCalendar's DateTime format.
        /// For example: 2008/1/26 will become 96/12/19
        /// </summary>
        public static DateTime ToTaiwanLunisolarDateTime(this DateTime d)
        {
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

            DateTime result = new DateTime(tlc.GetYear(d), tlc.GetMonth(d), tlc.GetDayOfMonth(d), d.Hour, d.Minute, d.Second, d.Millisecond);

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 取得農曆的日期
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns></returns>
        public static string GetLunisolarDate(DateTime dt)
        {
            string lunisolarDate        = "";
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

            if (tlc.IsLeapYear(tlc.GetYear(dt)) && tlc.GetMonth(dt) >= tlc.GetLeapMonth(tlc.GetYear(dt)))
            {
                lunisolarDate = FormatLunisolarYear(tlc.GetYear(dt)) +
                                FormatLunisolarMonth(tlc.GetMonth(dt) - 1) +
                                FormatLunisolarDay(tlc.GetDayOfMonth(dt));
            }
            else
            {
                lunisolarDate = FormatLunisolarYear(tlc.GetYear(dt)) +
                                FormatLunisolarMonth(tlc.GetMonth(dt)) +
                                FormatLunisolarDay(tlc.GetDayOfMonth(dt));
            }
            //return "農曆 " + lunisolarDate;
            return(lunisolarDate);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 西曆轉農曆
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToLunarDate(DateTime value)
        {
            var oLCalendar = new TaiwanLunisolarCalendar();

            var y = oLCalendar.GetYear(value);
            var m = oLCalendar.GetMonth(value);
            var d = oLCalendar.GetDayOfMonth(value);

            var l = y.ToString() +
                    StrFunc.StrPadLeft(m.ToString(), 2, '0') +
                    StrFunc.StrPadLeft(d.ToString(), 2, '0');

            return(l);
        }
Exemplo n.º 6
0
        public void TestDay()
        {
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

            int year  = tlc.GetYear(DateTime.Now);
            int month = tlc.GetMonth(DateTime.Now);
            int day   = tlc.GetDayOfMonth(DateTime.Now);

            string aa = string.Format("{0}/{1}/{2}", year, month, day);

            Console.WriteLine("day = " + aa);

            DateTime date2 = tlc.ToDateTime(year, month, day, 0, 0, 0, 0);

            Console.WriteLine("day2 = " + date2.ToShortDateString());
        }
Exemplo n.º 7
0
        /// <summary>
        /// 取得天干地支年月日
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetTaiwanHeaEarDate(this DateTime dt)
        {
            string teanGean             = "甲乙丙丁戊己庚辛壬癸";
            string deGe                 = "子丑寅卯辰巳午未申酉戌亥";
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();
            int lun60Year               = tlc.GetSexagenaryYear(DateTime.Now.AddYears(0));
            int TeanGeanYear            = tlc.GetCelestialStem(lun60Year) - 1;
            int DeGeYear                = tlc.GetTerrestrialBranch(lun60Year) - 1;
            int lunMonth                = tlc.GetMonth(DateTime.Now.AddYears(0));
            int leapMonth               = tlc.GetLeapMonth(tlc.GetYear(DateTime.Now.AddYears(0)));

            if (leapMonth > 0 && lunMonth >= leapMonth)
            {
                lunMonth -= 1;
            }
            int lunDay = tlc.GetDayOfMonth(DateTime.Now.AddYears(0));

            return(String.Format("{0}年{1}月{2}日", teanGean[TeanGeanYear].ToString() + deGe[DeGeYear].ToString(), lunMonth, lunDay));
        }
Exemplo n.º 8
0
        protected void Calendar01_SelectionChanged(object sender, EventArgs e)
        {
            using DataTable dtDate = new CglData().GetDateData(Calendar01.SelectedDate.Date);

            DataRow drDAte = dtDate.NewRow();

            // 找農曆
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();
            int leapMouth = tlc.GetLeapMonth(tlc.GetYear(Calendar01.SelectedDate.Date));
            int LuniMouth = tlc.GetMonth(Calendar01.SelectedDate.Date);

            if (leapMouth > 0)
            {
                if (LuniMouth == leapMouth)
                {
                    LuniMouth = leapMouth - 1;
                }
                else if (LuniMouth > leapMouth)
                {
                    LuniMouth -= 1;
                }
            }
            drDAte["lngDateSN"]  = string.Format(InvariantCulture, "{0}{1:d2}{2:d2}", Calendar01.SelectedDate.Year, Calendar01.SelectedDate.Month, Calendar01.SelectedDate.Day);
            drDAte["lngCDateSN"] = string.Format(InvariantCulture, "{0}{1:d2}{2:d2}", tlc.GetYear(Calendar01.SelectedDate.Date), LuniMouth, tlc.GetDayOfMonth(Calendar01.SelectedDate.Date));

            //找紫微
            StuPurple stuTemp = new StuPurple
            {
                StrWYear  = Calendar01.SelectedDate.Year.ToString(InvariantCulture),
                StrWMonth = Calendar01.SelectedDate.Month.ToString("d2", InvariantCulture),
                StrWDay   = Calendar01.SelectedDate.Day.ToString("d2", InvariantCulture),
                StrWHour  = "YY11",
                IntGender = CglPurple.Gender.MalePlus
            };

            stuTemp.Init();
            foreach (KeyValuePair <string, string> KeyPair in stuTemp.DicUpdateData)
            {
                if (KeyPair.Key != "lngDateSN")
                {
                    if (KeyPair.Value.Length >= 4)
                    {
                        drDAte[KeyPair.Key] = KeyPair.Value.Substring(0, 4);
                    }
                    else
                    {
                        drDAte[KeyPair.Key] = KeyPair.Value;
                    }
                }
            }

            //

            dtDate.Rows.Add(drDAte);
            GridView gvDate = new GalaxyApp().CreatGridView("gvDate", " gltabel ", dtDate, true, true);

            gvDate.DataBind();
            pnlCalendar.Controls.Add(gvDate);

            // 一些使用的範例程式
            //StringBuilder stringBuilder = new StringBuilder(0,8000);
            //for (int i = 2007; i <= 2050; i++)
            //{
            //    CNDate cd1 = new CNDate(new DateTime(i, Calendar01.SelectedDate.Date.Month, Calendar01.SelectedDate.Date.Day));
            //    stringBuilder.AppendLine("計算 " + i + "年隔年農曆過年的國曆日期:" + cd1.GetNextLunarNewYearDate().ToLongDateString() + "<br/>");
            //}

            //CNDate cd = new CNDate(Calendar01.SelectedDate);

            //stringBuilder.AppendLine("今年農曆天數:" + cd.LunarYearDays(2007).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	01	月農曆天數:" + cd.LunarMonthDays(2007, 1).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	02	月農曆天數:" + cd.LunarMonthDays(2007, 2).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	03	月農曆天數:" + cd.LunarMonthDays(2007, 3).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	04	月農曆天數:" + cd.LunarMonthDays(2007, 4).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	05	月農曆天數:" + cd.LunarMonthDays(2007, 5).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	06	月農曆天數:" + cd.LunarMonthDays(2007, 6).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	07	月農曆天數:" + cd.LunarMonthDays(2007, 7).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	08	月農曆天數:" + cd.LunarMonthDays(2007, 8).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	09	月農曆天數:" + cd.LunarMonthDays(2007, 9).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	10	月農曆天數:" + cd.LunarMonthDays(2007, 10).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	11	月農曆天數:" + cd.LunarMonthDays(2007, 11).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今年	12	月農曆天數:" + cd.LunarMonthDays(2007, 12).ToString(InvariantCulture) + "<br/>");
            //stringBuilder.AppendLine("今天的農曆日期:" + cd.GetLunarHolDay() + "<br/>");
            //stringBuilder.AppendLine("今年的農曆潤月月份:" + cd.GetLeapMonth(2007) + "<br/>");
            //stringBuilder.AppendLine("計算國曆當天對應的節氣:" + cd.LGetLunarHolDay() + "<br/>");
            //stringBuilder.AppendLine("計算今年農曆過年的國曆日期:" + cd.GetLunarNewYearDate().ToLongDateString() + "<br/>");
            //lblCC.Text = stringBuilder.ToString();
        }
Exemplo n.º 9
0
        /// <summary>
        /// 取得農曆年月日 (100年10月10日)
        /// </summary>
        public static string GetTaiwanLnsChiDate(this DateTime dt)
        {
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

            return(tlc.GetYear(dt) + "年" + tlc.GetMonth(dt) + "月" + tlc.GetDayOfMonth(dt) + "日");
        }
Exemplo n.º 10
0
        /// <summary>
        /// 取得農曆月
        /// </summary>
        public static int GetTaiwanLnsMonth(this DateTime dt)
        {
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

            return(tlc.GetMonth(dt));
        }
Exemplo n.º 11
0
    public string GetLunarDate(DateTime mdate, string mtype)
    {
        String_Func sfc = new String_Func();
        TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

        string ldate = "";
        int LunarYear = 0;										// 農曆年
        int LunarMonth = 0;										// 月份
        int LunarDay = 0;										// 日期
        int LunarHour = 0;										// 時
        int LunarMin = 0;										// 分
        int LunarSec = 0;										// 秒
        int LeapMonth = 0;										// 潤月

        LunarYear = tlc.GetSexagenaryYear(mdate);				// 取得西元年

        #region 農曆年
        if (mtype.Contains("y"))
        {
            ldate = GetHeavenlyStem(tlc.GetCelestialStem(LunarYear));					// 年 - 天干
            ldate += GetEarthlyBranch(tlc.GetTerrestrialBranch(LunarYear)) + "年";		// 年 - 地支
        }
        #endregion

        #region 農曆月
        if (mtype.Contains("M"))
        {
            LunarMonth = tlc.GetMonth(mdate);						// 取得月份
            LeapMonth = tlc.GetLeapMonth(tlc.GetYear(mdate));		// 取得潤月

            if (LeapMonth > 0)
            {
                // 當年有潤月,月份會出現13個月,在潤月之後的月分要減一。
                if (LeapMonth == LunarMonth)
                {
                    ldate += "閏" + GetChMonth(LeapMonth - 1) + "月";
                }
                else if (LunarMonth > LeapMonth)
                {
                    ldate += GetChMonth(LunarMonth - 1) + "月";
                }
                else
                    ldate += GetChMonth(LunarMonth) + "月";
            }
            else
                ldate += GetChMonth(LunarMonth) + "月";
        }
        #endregion

        #region 農曆日
        if (mtype.Contains("d"))
        {
            LunarDay = tlc.GetDayOfMonth(mdate);

            ldate += GetChDay(LunarDay) + "日";
        }
        #endregion

        #region 農曆時 (子、丑...)
        if (mtype.Contains("H"))
        {
            LunarHour = tlc.GetHour(mdate);
            ldate += GetChHour(LunarHour) + "時";
        }
        #endregion

        #region 中文數字時 (五、十一...)
        if (mtype.Contains("h"))
        {
            LunarHour = tlc.GetHour(mdate);
            ldate += GetChNHour(LunarHour) + "時";
        }
        #endregion

        #region 農曆分
        if (mtype.Contains("m"))
        {
            LunarMin = tlc.GetMinute(mdate);
            ldate += sfc.GetChNumber((ulong)LunarMin).Replace("一十", "十") + "分";
        }
        #endregion

        #region 農曆秒
        if (mtype.Contains("s"))
        {
            LunarSec = tlc.GetSecond(mdate);
            if (LunarSec == 0)
            {
                ldate += "整";
            }
            else
            {
                ldate += sfc.GetChNumber((ulong)LunarSec).Replace("一十", "十") + "秒";
            }
        }
        #endregion

        return ldate;
    }